有 Java 编程相关的问题?

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


共 (3) 个答案

  1. # 1 楼答案

    EntityManagerFactory在每次调用EntityManagerFactory时返回EntityManager的新实例。createEntityManager()

    如果你执行

    emf = Persistence.createEntityManagerFactory("basicPU");
     for (int i = 0 ; i<10; i++){
         System.out.println(em = emf.createEntityManager());
     }
    

    它打印:

    org.apache.openjpa.persistence.EntityManagerImpl@18105e8
    org.apache.openjpa.persistence.EntityManagerImpl@9bad5a
    org.apache.openjpa.persistence.EntityManagerImpl@91f005
    org.apache.openjpa.persistence.EntityManagerImpl@1250ff2
    org.apache.openjpa.persistence.EntityManagerImpl@3a0ab1
    org.apache.openjpa.persistence.EntityManagerImpl@940f82
    org.apache.openjpa.persistence.EntityManagerImpl@864e43
    org.apache.openjpa.persistence.EntityManagerImpl@17c2891
    org.apache.openjpa.persistence.EntityManagerImpl@4b82d2
    org.apache.openjpa.persistence.EntityManagerImpl@179d854
    
  2. # 2 楼答案

    这个Javadoc是明确的:

    Create a new application-managed EntityManager. This method returns a new EntityManager instance each time it is invoked.

  3. # 3 楼答案

    对于skaffman的第二个答案,这里是JPA1.0规范的摘录:

    5.9.2 Provider Responsibilities

    The Provider has no knowledge of the distinction between transaction-scoped and extended persistence contexts. It provides entity managers to the container when requested and registers for synchronization notifications for the transaction.

    • When EntityManagerFactory.createEntityManager is invoked, the provider must create and return a new entity manager. If a JTA transaction is active, the provider must register for synchronization notifications against the JTA transaction.