简体   繁体   English

JPA自动加载的惰性集合

[英]JPA auto loaded lazy collection

I have two entity with relationship following: 我有两个具有以下关系的实体:

public class Category implements Serializable {
     @Column(name = "caID")
     private Integer caID;
     @OneToMany(cascade=CascadeType.ALL,mappedBy="caID",fetch=FetchType.LAZY)
     private List<Product> productList;
     //..... GET and SET        
}
public class Product implements Serializable {
     @Column(name = "pID")
     private Integer pID;
     @JoinColumn(name = "caID", referencedColumnName = "caID")
     @ManyToOne(optional = false, fetch = FetchType.EAGER)
     private Category caID;
     //..... GET and SET        
}

Category Model: 类别型号:

private List<Category> findCategoryEntities(boolean all, int maxResults, int firstResult) {
    EntityManagerFactory eMF = null;
    EntityManager eM = null;
    try {
        eMF = getEntityManagerFactory();
        eM = getEntityManager(eMF);
        Query q=eM.createQuery("SELECT ca FROM Category ca");
        //Or using Criteria
        //CriteriaQuery cq = eM.getCriteriaBuilder().createQuery();
        //cq.select(cq.from(Category.class));
        //Query q = eM.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } finally {
        closeConnection(eM, eMF);
    }
}

I think if I set fetch loading of listProduct is lazy type, this property won't auto loaded by Entity Manager. 我认为如果我将listProduct的获取加载设置为惰性类型,则此属性将不会由实体管理器自动加载。 But when I call method 'findCategoryEntities' and check productList, it's not null? 但是,当我调用方法“ findCategoryEntities”并检查productList时,它不为null吗? After I use Criteria and I still get the same result. 使用Criteria之后,我仍然得到相同的结果。 How can I get all Category but not auto initialize productList? 如何获取所有类别,但不能自动初始化productList? And How can I set productList after that but not initialize Category again? 然后如何设置productList但又不能再次初始化Category?

When you mark a child as lazy, and load the parent, the child list will never be null. 当您将一个子级标记为懒惰并加载父级时,子级列表将永远不会为空。 It will return you proxy to the list of child objects. 它将使您返回到子对象列表的代理。 So when you do getProductList it will call the get on the proxy which will end up loading your children. 因此,当您执行getProductList时,它将调用代理上的get,这将最终加载您的孩子。

With the configuration you have your productList wont load when you fetch all Category rest assured, it will only load when you access the getProductList. 使用该配置时,放心获取所有类别时,不会加载productList,只有在访问getProductList时才会加载。

[update] You can follow the url and read the section "How Lazy Loading Works in Hibernate" Hibernate and Lazy loading . [更新]您可以按照url并阅读“延迟加载如何在Hibernate中工作”部分的休眠和延迟加载 That might help you clarify the lazy loading. 这可能有助于您阐明延迟加载。 If you access lazy loaded elements after closing the hibernate session in which it is loaded you will end up with LazyInitializationException 如果您在关闭休眠的会话后访问延迟加载的元素,则将导致LazyInitializationException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM