Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Hibernate @OneToMany Criteria Projections returning NULL
    primarykey
    data
    text
    <p>I have One to many relationship Owner->Dog.</p> <p>I am to query the dog by ID as well bring the Owner in EAGER way I have set this code using Hibernate 4.1.6 not XML mapping is used. i only need some fields from DOG and OWNER using Projections the SQL being generated by Hibernate is perfect but my objects is not being populate because DOG is returning the fields populated but owner is returned <code>DOG.OWNER==NULL</code> here is the code i am using so far... My <code>entities.other</code> code is omit by brevity</p> <pre><code>@Entity public class Owner implements Serializable { Set&lt;Dog&gt;dogs=new HashSet&lt;Dogs&gt;(0); @OneToMany(fetch=FetchType.LAZY, mappedBy="owner") public Set&lt;Dogs&gt; getDogs(){return this.dogs} } @Entity public class Dog implements Serializable { private Owner owner; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="ownerid") public Owner getOwner(){return this.owner;} } </code></pre> <p>here is my method.</p> <pre><code>public Dog getDogAndOwnerById(Integer dogId) { Projection p=Projections.projectionList() .add(Projections.property("d.id"),"id") .add(Projections.property("o.id"),"id");//and others fields Session session = getHibernateTemplate().getSessionFactory().openSession(); Criteria like = session.createCriteria(Dog.class,"d") .add(Restrictions.idEq(dogId)).setProjection(p) .setResultTransformer(Transformers.aliasToBean(Dog.class)) .setFetchMode("owner",FetchMode.JOIN).createAlias("owner","o"); Dog dog = (Dog)like.uniqueResult(); //Object[]obj=(Object[])like.uniqueResult(); for(Object id:obj)System.out.println(id); //System.out.println(obj.length); session.close(); return dog; //dog is OK BUT dog.OWNER is null. } </code></pre> <p>the query is perfect here is the SQL </p> <pre><code>select this_.ID as y0_, owner_.ID as y1_ from dog this_ inner join owner owner_ on this_.ownerid=owner_.ID where and this_.ID = ? </code></pre> <p>my problem is... Dog instance is NOT null and all the fields are O.K meanwhile <code>Dog.Owner</code> is returnig null I have try this not using any Transformers.</p> <pre><code>Object[]obj=(Object[])like.uniqueResult(); for(Object id:obj)System.out.println(id); System.out.println(obj.length); </code></pre> <p>And I can see the data correct what Hibernate is not returning my objects right? What I am doing wrong.</p> <p>any help is hugely appreciate.</p> <p>[update] if i use this</p> <pre><code>Projection p=Projections.projectionList() .add(Projections.property("d.id"),"id") .add(Projections.property("o.status"),"status"); </code></pre> <p>and status belongs to both tables the DOG entity is populate the other is not.</p> <p>if i use</p> <pre><code>Projection p=Projections.projectionList() .add(Projections.property("d.id"),"id") .add(Projections.property("o.address"),"address"); </code></pre> <p>and adress belong only to owner exception is thrown.</p> <pre><code>Exception in thread "main" org.hibernate.PropertyNotFoundException: Could not find setter for address on class com.generic.model.Dog </code></pre> <p>Seems that Hibernate ALWAYS return at maximun 1 entity populate they can't populate both tables[selected columns] into objects[selected objects]?</p>
    singulars
    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.
 

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