Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Hibernate property Projection to fetch association and inject it to Dto
    primarykey
    data
    text
    <p>I have a Entity (MediaCostRule) that has several regular fields and one association fields annotated with @ElementCollection (MediaCostRuleSource)</p> <p>I also have a dto (MediaCostRuleDto) that holds subset of the Entity fields and also includes the same association set</p> <p>Here is the classes code</p> <pre><code>@Entity @Table(name = "media_cost_rules") public class MediaCostRule extends AdminEntity{ @Id @GeneratedValue @Column(name = "media_cost_rule_id") private Integer id; @Column(name = "name") @Length(max = 100) private String name; @Column(name="is_enabled") private Boolean enabled; @ManyToOne @JoinColumn(name = "brand_id") private Brand brand; @ElementCollection(fetch = FetchType.EAGER) @CollectionTable(name = "media_cost_rule_sources", joinColumns = @JoinColumn(name = "media_cost_rule_id")) private Set&lt;MediaCostRuleSource&gt; mediaCostRuleSources = new HashSet&lt;MediaCostRuleSource&gt;(); </code></pre> <p>=====================================================================</p> <pre><code>@Embeddable public class MediaCostRuleSource { @Column(name = "source_name") private String sourceName; @Column(name = "ordinal") private Integer ordinal; </code></pre> <p>==================================================================</p> <pre><code>public class MediaCostRuleDto extends AbstractAdminDto { @Size(min = 1) private Set&lt;MediaCostRuleSource&gt; mediaCostRuleSources = new HashSet&lt;MediaCostRuleSource&gt;(); @NotNull private String name; </code></pre> <p>===========================================================================</p> <p>Now what I would like is to use Projections in order to fetch just the mediaCostRuleSources and name of my entity and to have it injected directly to my MediaCostRuleDto (using AliasToBeanResultTransformer).</p> <p>I have tried the following code:</p> <pre><code>@Test public void findByIdDirectTest2(){ Criteria crit = sessionFactory.getCurrentSession().createCriteria(MediaCostRule.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("name").as("name")); projectionList.add(Projections.property("mediaCostRuleSources").as("mediaCostRuleSources")); crit.setProjection(projectionList); List list = crit.list(); } </code></pre> <p>But I am always getting exceptions (ArrayIndexOutOfBoundsException)</p> <p>There must be some kind of way to do such thing with Hibernate</p> <p>Do you know how ??</p> <p>Yosi</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload