JPA / Hibernate代理不获取真实对象数据,将所有属性设置为null [英] JPA/Hibernate proxy not fetching real object data, sets all properties to null
问题描述
我在JPA中使用Hibernate并且有如下关系:
public class PencilImpl implements Pencil {
@ManyToOne(targetEntity = PersonImpl.class,fetch = FetchType.LAZY)
@JoinColumn(name =owner,nullable = false)
私人所有者;
...
@Override
public final Person getOwner(){
return owner;
$ b 自从我开始使用LAZY获取类型以来,每次我尝试获取铅笔的所有者( pencil.getOwner )我得到一个非null对象,它的所有内部属性都设置为null。
我看起来像由Hibernate创建的代理不会从数据库中提取实际的对象。
任何想法?谢谢:)
解决方案正如JB Nizet所建议的,我的类的getters中的 final 修饰符是messing用hibernate为懒惰加载关系创建的代理。
I'm using Hibernate with JPA and have a relationship that looks like this:
public class PencilImpl implements Pencil {
@ManyToOne(targetEntity = PersonImpl.class, fetch = FetchType.LAZY)
@JoinColumn(name = "owner", nullable = false)
private Person owner;
...
@Override
public final Person getOwner() {
return owner;
}
}
Since I started using the LAZY fetch type, everytime I try to get a pencil's owner (pencil.getOwner) I get a non-null object that has all of it's inner properties set to null.
I looks like the proxy created by Hibernate is not fetching the real object from the database when it should.
Any ideas? Thanks :)
解决方案 As JB Nizet suggested, the final modifier in my classes' getters was messing with the proxies hibernate creates for lazy loaded relationships.
这篇关于JPA / Hibernate代理不获取真实对象数据,将所有属性设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!