gpt4 book ai didi

java - JPA持久化上下文什么时候结束?

转载 作者:行者123 更新时间:2023-11-30 06:27:32 25 4
gpt4 key购买 nike

通读Java Persistence Entity Operations ATM,里面有以下语句:

JPA provides support for this pattern by automatically detaching entities when they are serialized or when a persistence context ends.

持久化上下文什么时候结束?是在每笔交易结束时吗?

最佳答案

当调用 EntityManager.close() 时,持久化上下文结束。 Here's an example .

取自问题中提供的链接的另一个示例:

MagazineId mi = new MagazineId();
mi.isbn = "1B78-YU9L";
mi.title = "JavaWorld";

// updates should always be made within transactions; note that
// there is no code explicitly linking the magazine or company
// with the transaction; JPA automatically tracks all changes
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Magazine mag = em.find(Magazine.class, mi);
mag.setPrice(5.99);
Company pub = mag.getPublisher();
pub.setRevenue(1750000D);
em.getTransaction().commit();

// or we could continue using the EntityManager...
em.close();

关于java - JPA持久化上下文什么时候结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com