Hibernate不会初始化代理 [英] Hibernate won't initialize proxy

查看:141
本文介绍了Hibernate不会初始化代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate中有这个映射(通过JPA,使用EntityManager等):

  public ChildClass {

...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =parentClassId)
private ParentClass parentClass;
....
}

和一个拥有列表的父类 ChildClass 元素:

  public ParentClass {
.. 。
@OneToMany(mappedBy =parentClass,fetch = FetchType.LAZY)
private List< ChildClass> childElements;
...

}

现在我有这种方法在我的服务层:

  @Transactional 
public ChildClassDTO consultarPuntuacionPrueba(Long parentClassId){
ChildClass result = childClassDAO.getChildClassByParentId(parentClassId);



ParentClass parentClass = result.getParentClass();

System.out.println(UNITIALIZED:+((HibernateProxy)prueba).getHibernateLazyInitializer()。isUninitialized());


长的parentClassId = parentClass.getParentClassId();
布尔anAttribute = parentClass.getBooleanAttribute();

返回地图(结果,ChildClassDTO.class);
}

问题是 - parentClass是一个javassist代理,即使访问一个getter parentClass ,我可以看到一个正在执行的查询(它显示在我的控制台中),代理永远不会被初始化,并为所有属性保留为空...我试过我知道当我调用 childClass.getParentClass()时,Hibernate必须返回一个代理,并且它会返回预期的数据。

,但是为什么它之后没有初始化?

解决方案

确保你不使用 final 关键字为您的实体getter和setter,否则代理将无法覆盖这些方法并可能无法正常工作。


I've got this mappings in Hibernate (through JPA, using EntityManager and such):

 public ChildClass {

     ....
      @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "parentClassId")
        private ParentClass parentClass;
     ....
    }

And a parent class that has a list of ChildClass elements:

 public ParentClass {
    ...
    @OneToMany(mappedBy = "parentClass", fetch = FetchType.LAZY)
        private List<ChildClass> childElements;
    ...

    }   

Now I have this method in my Service layer:

@Transactional
public ChildClassDTO consultarPuntuacionPrueba(Long parentClassId) {
            ChildClass result= childClassDAO.getChildClassByParentId(parentClassId);



            ParentClass parentClass = result.getParentClass();

            System.out.println("UNITIALIZED: " + ((HibernateProxy)prueba).getHibernateLazyInitializer().isUninitialized());


            Long parentClassId = parentClass.getParentClassId();
            Boolean anAttribute = parentClass.getBooleanAttribute();

            return map(result, ChildClassDTO.class);
        }

The problem is -- parentClass is a javassist proxy, and even though when accessing a getter of parentClass, I can see a query getting executed (it shows in my console), the proxy never gets initialized and holds null for all attributes... I have tried said query directly towards my database, and it returns the expected data.

I know Hibernate must return a proxy when I call childClass.getParentClass(), but why is it never initialized afterwards?

解决方案

Make sure your don't use the final keyword for your entities getters and setters otherwise the proxies will not be able to override the methods and may not work properly.

这篇关于Hibernate不会初始化代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆