[英]JPA how to lazily load collection?
請告知如何延遲加載 map“Groupunit_from”? 如何開一個 JPA session 和交易?
@ElementCollection(targetClass=String.class,fetch=FetchType.LAZY)
@CollectionTable(name="place_definer_groupunit_from",joinColumns=@JoinColumn(name="groupunit_from"))
@MapKeyColumn(name="id")
@MapKeyClass(String.class)
@Column(name="ordinal",nullable=false)
private Map<String,String> Groupunit_from=new HashMap<>();//
Collections默認是懶加載的。
您無需指定任何內容即可延遲加載集合。
初始化 session 和 JPA 中的事務
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistentUnitName");
EntityManager em = emf.createEntityManager();
// to begin a transaction
em.getTransaction().begin();
// here you can flush or persist
// to commit a transaction
em.getTransaction().commit();
您已經提到“fetch=FetchType.LAZY”,所以 jpa 將延遲加載這些元素。 如果您在調試時觀察應用程序中觸發的查詢,您將觀察到當您在 Z93F725A07423FE1C889F448B33D21F46 代碼中首次使用此 map 時,將觸發填充此 map 的查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.