Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过方法调用在Hibernate中启用即时初始化_Java_Spring_Hibernate - Fatal编程技术网

Java 如何通过方法调用在Hibernate中启用即时初始化

Java 如何通过方法调用在Hibernate中启用即时初始化,java,spring,hibernate,Java,Spring,Hibernate,hibernate中加载的默认实体设置为lazy。我想在Hibernate中启用即时初始化,在调用方法的同时仍然使用延迟加载: 例如,我有如下实体: public class Applications implements java.io.Serializable { private int id; private String name; private Set viewses = new HashSet(0); // here entities view

hibernate中加载的默认实体设置为lazy。我想在Hibernate中启用即时初始化,在调用方法的同时仍然使用延迟加载:

例如,我有如下实体:

 public class Applications  implements java.io.Serializable {


     private int id;
     private String name;
     private Set viewses = new HashSet(0); // here entities views
     private Set routeses = new HashSet(0);  // here antoher entities routes

     public Set getViewses() {
        return this.viewses;
     }

     public void setViewses(Set viewses) {
        this.viewses = viewses;
     }

     public Set getRouteses() {
        return this.routeses;
     }

     public void setRouteses(Set routeses) {
        this.routeses = routeses;
     }

     // .... other things

 } 
我希望避免迭代所有inners对象,以完全获取有关对象应用程序的所有详细信息,例如:

        begin();
        List apps = getSession().createQuery("from Applications").list(); // here lazy initalisation not all inner elements are filled from database
        commit();
上面的代码导致内部实体(如管线、视图)为空。 当我调用application.getRouteses()时,什么也不会发生,因为我已将lazy设置为true,并在会话关闭(提交)后调用getRouteses()。通过一个事务设置实体应用程序中的所有元素并从方法完全返回的唯一方法是使用迭代和设置器或即时初始化:

        begin();
        List apps = getSession().createQuery("from Applications").list();
        Iterator iter = apps.iterator();
        while(iter.hasNext()){
            Applications application = (Applications) iter.next();
            application.setRouteses(application.getRouteses()); // here i set all routes beacuse i call getRoutes and hibernate will load from db
        }
        commit();
现在,下面的代码加载有关对象的所有详细信息,包括内部路由和视图实体:

        begin();
        List apps = getSession().createQuery("from Applications").list(); // here eager all inner object are filled from databasse
        commit();
唯一的问题是将lazy更改为eager,但在hibernate.xml配置中没有。我想知道有没有可能先打开“渴望”一段时间,然后打开“懒惰”(编程)。例如:

        // HERE LAZY INITIALISATION
        turnOnEager(); // some method ???

        // HERE EAGER INITIALISATION
        begin();

        List apps = getSession().createQuery("from Applications").list(); // here eager all inner object are filled from databasse
        commit();

        // AND LAZY AGAIN
        turnOnLazy(); // some method ???

您不能在运行时打开和关闭懒惰。但是,您可以尝试更改hql以使用fetch关键字获取子实体,如下所示

HQL

from Applications a join fetch a.someProperty s join fetch s.anotherProperty
从文档:

“fetch”联接允许创建值的关联或集合 使用单个选择与其父对象一起初始化。 这在集合的情况下特别有用。信息技术 有效地重写 关联和集合的映射文件

参考: