问题描述
我需要在我的hibernate映射中设置获取模式,以便在某些情况下非常渴望,而在其他情况下则很懒惰。我有我的默认值(通过hbm文件设置)为lazy =true。如何在代码中覆盖此设置? MyClass有一个为MyClass2定义的集合,我想将FetchMode设置为EAGER。
目前,我有类似的东西:
Session s = HibernateUtil.getSessionFactory()。openSession();
MyClass c =(MyClass)session.get(MyClass.class,myClassID);
您可以尝试如下所示:
pre $ code> Criteria crit = session.createCriteria(MyClass.class);
crit.add(Restrictions.eq(id,myClassId));
crit.setFetchMode(myProperty,FetchMode.EAGER);
MyClass myThingy =(MyClass)crit.uniqueResult();
我相信应该使用FetchMode.JOIN或FetchMode.SELECT来代替FetchMode.EAGER。
I need to set the fetch mode on my hibernate mappings to be eager in some cases, and lazy in others. I have my default (set through the hbm file) as lazy="true". How do I override this setting in code? MyClass has a set defined of type MyClass2 for which I want to set the FetchMode to EAGER.
Currently, I have something like:
Session s = HibernateUtil.getSessionFactory().openSession();
MyClass c = (MyClass)session.get(MyClass.class, myClassID);
You could try something like this: (code off the top of my head)
Criteria crit = session.createCriteria(MyClass.class);
crit.add(Restrictions.eq("id", myClassId));
crit.setFetchMode("myProperty", FetchMode.EAGER);
MyClass myThingy = (MyClass)crit.uniqueResult();
I believe that FetchMode.JOIN or FetchMode.SELECT should be used instead of FetchMode.EAGER, though.
这篇关于在原生Hibernate中设置FetchMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!