有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java确实支持PersistenceContextType。延长?

@Stateful EJB可以使用PersistenceContextType。扩展用于跨多个事务重用同一EntityManager。这在实现对话范围时很有用。Spring对此有任何支持吗


共 (3) 个答案

  1. # 1 楼答案

    Spring有一个带有静态工厂方法的特殊类,用于创建扩展实体管理器。见ExtendedEntityManagerCreator

    此外,如果您注入一个EntityManagerFactory并调用createEntityManager(SynchronizationType.SYNCHRONIZED),它将创建具有扩展持久性上下文的实体管理器(在提交事务后,它不会自动分离实体)

  2. # 2 楼答案

    Implementing DAOs based on plain JPA中有一个简短的讨论:

    The @PersistenceContext annotation has an optional attribute type, which defaults to PersistenceContextType.TRANSACTION. This default is what you need to receive a shared EntityManager proxy. The alternative, PersistenceContextType.EXTENDED, is a completely different affair: This results in a so-called extended EntityManager, which is not thread-safe and hence must not be used in a concurrently accessed component such as a Spring-managed singleton bean. Extended EntityManagers are only supposed to be used in stateful components that, for example, reside in a session, with the lifecycle of the EntityManager not tied to a current transaction but rather being completely up to the application.

    所以不,听起来不像是弹簧支撑着它们