java - 如何通过方法调用在 Hibernate 中打开急切初始化

标签 java spring hibernate

hibernate 中加载的默认实体设置为惰性。我想在方法调用时打开 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

 } 

我想避免迭代所有内部对象以完全获取有关对象应用程序的所有详细信息例如:

        begin();
        List apps = getSession().createQuery("from Applications").list(); // here lazy initalisation not all inner elements are filled from database
        commit();

以上代码导致内部实体(例如(路线, View ))为空。 当我调用 application.getRouteses() 时,没有任何反应,因为我已将惰性设置为 true,并且在 session 关闭(提交)后调用 getRouteses() 通过一个事务设置实体应用程序中的所有元素并从方法中完全返回它的唯一方法是使用迭代和 setter 或急切初始化:

        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();

现在急于启动,下面的代码也加载有关具有内部路由和 View 实体的对象的所有详细信息:

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

唯一的问题是将惰性更改为急切,但不在 hibernate.xml 配置中。我想知道是否可以先开启 eager 一段时间,然后再开启懒惰(编程)。例如:

        // 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

来自文档:

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.

引用号:http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html

关于java - 如何通过方法调用在 Hibernate 中打开急切初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24739922/

相关文章:

java - 使用 Spring Boot 显示的动态列数

java - 在 springboot aws lambda 中注入(inject)服务

java - Spring:如何避免自动解析特定占位符的属性

hibernate - 在 HQL 中加入但不关联

java - 我们能否将延迟和吞吐量与并行性联系起来

java内省(introspection)查找类的成员

java - 为什么空集(如 {} 中所示)是所有集合的子集? ( java )

java - 速度零点异常

java - MyBatis 可以创建数据库模式吗?

java - 是否可以使用 Hibernate Validator 设置验证顺序?