[英]Java Hibernate @OneToMany Criteria Projections returning NULL
I have One to many relationship Owner->Dog. 我有一对多关系所有者 - >狗。
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. 我将通过ID查询狗以及以EAGER方式引入所有者我使用Hibernate 4.1.6设置此代码而不使用XML映射。 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
DOG.OWNER==NULL
here is the code i am using so far... My entities.other
code is omit by brevity 我只需要DOG和OWNER的一些字段使用Projections Hibernate生成的SQL是完美的但是我的对象没有填充,因为DOG返回填充的字段但是返回了所有者
DOG.OWNER==NULL
这里是我正在使用的代码到目前为止...我的entities.other
代码简洁省略
@Entity
public class Owner implements Serializable
{
Set<Dog>dogs=new HashSet<Dogs>(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="owner")
public Set<Dogs> 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;}
}
here is my method. 这是我的方法。
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.
}
the query is perfect here is the SQL 这里的查询很完美就是SQL
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 = ?
my problem is... Dog instance is NOT null and all the fields are OK meanwhile Dog.Owner
is returnig null I have try this not using any Transformers. 我的问题是...狗实例不是null并且所有字段都可以,同时
Dog.Owner
是Dog.Owner
null我试过这个不使用任何变形金刚。
Object[]obj=(Object[])like.uniqueResult(); for(Object id:obj)System.out.println(id);
System.out.println(obj.length);
And I can see the data correct what Hibernate is not returning my objects right? 我可以看到数据正确Hibernate没有返回我的对象吗? What I am doing wrong.
我做错了什么。
any help is hugely appreciate. 任何帮助都非常感激。
[update] if i use this [更新]如果我使用这个
Projection p=Projections.projectionList()
.add(Projections.property("d.id"),"id")
.add(Projections.property("o.status"),"status");
and status belongs to both tables the DOG entity is populate the other is not. 和状态属于两个表,DOG实体填充另一个不是。
if i use 如果我使用
Projection p=Projections.projectionList()
.add(Projections.property("d.id"),"id")
.add(Projections.property("o.address"),"address");
and adress belong only to owner exception is thrown. 和地址只属于所有者异常被抛出。
Exception in thread "main" org.hibernate.PropertyNotFoundException:
Could not find setter for address on class com.generic.model.Dog
Seems that Hibernate ALWAYS return at maximun 1 entity populate they can't populate both tables[selected columns] into objects[selected objects]? 似乎Hibernate总是返回最大值1实体填充它们不能将两个表[selected columns]填充到对象[selected objects]中?
i follow this post Complex Hibernate Projections and as result i write my own Transformer because resultTransformer alias to bean in hibernate in one level deep. 我遵循这篇文章复杂的Hibernate预测 ,因此我编写了自己的Transformer,因为resultTransformer在一个级别深度的hibernate中为bean做了别名。
here is my simple resultTransformer 这是我简单的resultTransformer
public class MyOwnTransformer implements ResultTransformer
{
@Override//the same order in projection list properties is the same returned by data array...
public Dog transformTuple(Object[]data,String[]alias)
{return new Dog((Integer)data[0],new Owner((Integer)data[1]));}
@Override
public List transformList(List dogs){return dogs;}//nothing to do here....
}
and in my criteria i fix the code like this. 并在我的标准我修复这样的代码。
public Dog getDogAndOwnerById(Integer dogId)
{
Projection p=Projections.projectionList()
.add(Projections.property("d.id"))//i dont need the alias anymore..
.add(Projections.property("o.id"));
Session session = getHibernateTemplate().getSessionFactory().openSession();
Criteria like = session.createCriteria(Dog.class,"d")
.add(Restrictions.idEq(dogId)).setProjection(p)
.setResultTransformer(new MyOwnTransformer())
.setFetchMode("owner",FetchMode.JOIN).createAlias("owner","o");
Dog dog = (Dog)like.uniqueResult();
session.close();
return dog;
}
i hope it helps somebody. 我希望它对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.