2017-04-18 101 views
2

我得到了一個典型的實體關聯的訂單和項目。爲了只能讀取訂單,設置的項目是默認的FetchType.LAZY。第二級和查詢緩存已啓用。要閱讀與相關項目的訂單,我正在使用JPQL查詢。查詢和實體由EHCache緩存。但是在訪問項目時的第二次調用中,拋出了LazyInitializationException,因爲項目未初始化(未從緩存中恢復)。爲什麼?實施此要求的最佳方式是什麼?休眠緩存和延遲加載的關聯

訂單:

@Entity 
@Cacheable 
@NamedQueries({ 
    @NamedQuery(name = Order.NQ_FIND_BY_ID_FETCH_ITEMS, query = "SELECT DISTINCT o FROM Order o JOIN FETCH o.items WHERE o.id = :id") 
}) 
@Table(...) 
public class Order extends ... { 
    ... 
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) 
    // @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
    private Set<Item> items = new HashSet<Item>(); 
    ... 
} 

項目:

@Entity 
@Cacheable 
@Table(...) 
public class Item extends ... { 
    @ManyToOne 
    @JoinColumn(name = "order_id", nullable = false) 
    private Order order; 
    ... 
} 

DAO:

public class OrderDaoJpaImpl extends ... { 

    @Override 
    public Catalog findByIdFetchItems(Long id) { 
    TypedQuery<Order> query = entityManager.createNamedQuery(Order.NQ_FIND_BY_ID_FETCH_ITEMS, Order.class); 
    query.setParameter("id", id); 
    // query.setHint(QueryHints.HINT_CACHEABLE, Boolean.TRUE); 
    Order order = JPAUtil.singleResultOrNull(query); 
    return order; 
} 

服務:

@Service("orderService") 
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 
public class OrderServiceImpl implements OrderService { 

    @Override 
    public Order getOrderWithItems(Long orderId) { 
    return orderDao.findByIdFetchItems(orderId); 
    } 
} 

persistence.xml:

<persistence ...> 
    <persistence-unit name="shop-persistence" transaction-type="RESOURCE_LOCAL"> 
    <jar-file>shop-persistence.jar</jar-file> 
    <!-- Enable JPA 2 second level cache --> 
    <shared-cache-mode>ALL</shared-cache-mode> 
    <properties> 
     ... 
     <property name="hibernate.cache.use_second_level_cache" value="true" /> 
     <property name="hibernate.cache.use_query_cache" value="true" /> 
     <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

Spring Framework 4.3.7.RELEASE和Hibernate 5.2.9.Final。如你所見,我試過使用Hibernate實體註釋和緩存提示,而不是JPA緩存。我也嘗試過JPA實體圖而不是JOIN FETCH。總是相同的:在訂單查詢的第二次調用時,項目不會被初始化/恢復。

+0

我是唯一一個使用Hibernate/JPA緩存與多對多關係嗎? – Titus

回答

0

我認爲@Cacheable不是Hibernate緩存,而是Spring緩存。 @Cache註釋用於Hibernate緩存。 除此之外,當我遇到麻煩時,爲了使JOIN FETCH結果也可以用於緩存,我必須將Hibernate.initialize(...)添加到Dao方法以避免LazyInitializationException。