Persistence,EntityManagerFactory和EntityManager

来源:互联网 发布:淘宝信用支付怎么开通 编辑:程序博客网 时间:2024/06/02 05:07
  1.持久化上下文 

  1. 持久化单元(persist unit就是关于一组Entity的命名配置。持久化单元是一个静态概念。
  2. 持久化上下文(Persist Context就是一个受管的Entity实例的集合。
  3. 每一个持久化上下文都关联一个持久化单元,持久化上下文不可能脱离持久化单元独立存在。
  4. 持久化上下文是一个动态概念。
  5. 尽管持久化上下文非常重要,但是开发者不直接与之打交道,持久化上下文在程序中是透明的,我们通过EntityManager间接管理它。

    2.三个类:Persistence,EntityManagerFactory和EntityManager

  1. 实体管理器工厂 EntityManagerFactory是获得实体管理器EntityManager对象的入口,
  2. EntityManagerFactory对象是通过javax.persistenc.Persistence类中的静态方法createEntityManagerFactory来创建的。

javax.persistenc.Persistence API 

  • javax.persistenc.Persistence类提供了两个手动创建EntityManagerFactory对象的方法,它们的定义如下所示。 
  1. package javax.persistence;
  2. import java.util.*;
  3. public class Persistence {
  4. public static EntityManagerFactory createEntityManagerFactory(
  5. String persistenceUnitName) {...}
  6. public static EntityManagerFactory createEntityManagerFactory(
  7. String persistenceUnitName, Map properties) {...}
  8. }

  • 其中,createEntityManagerFactory(String persistenceUnitName, Map properties)方法中properties的参数将覆盖persistence.xml文件配置的参数。 

EntityManagerFactory API 

  1. package javax.persistence;
  2. import java.util.Map;
  3. import javax.persistence.metamodel.Metamodel;
  4. import javax.persistence.criteria.CriteriaBuilder;
  5. /**
  6. * Interface used to interact with the entity manager factory
  7. * for the persistence unit.
  8. *
  9. * <p>When the application has finished using the entity manager
  10. * factory, and/or at application shutdown, the application should
  11. * close the entity manager factory. Once an
  12. * <code>EntityManagerFactory</code> has been closed, all its entity managers
  13. * are considered to be in the closed state.
  14. *
  15. * @since Java Persistence 1.0
  16. */
  17. public interface EntityManagerFactory {
  18. /**
  19. * Create a new application-managed <code>EntityManager</code>.
  20. * This method returns a new <code>EntityManager</code> instance each time
  21. * it is invoked.
  22. * The <code>isOpen</code> method will return true on the returned instance.
  23. * @return entity manager instance
  24. * @throws IllegalStateException if the entity manager factory
  25. * has been closed
  26. */
  27. public EntityManager createEntityManager();
  28. /**
  29. * Create a new application-managed <code>EntityManager</code> with the
  30. * specified Map of properties.
  31. * This method returns a new <code>EntityManager</code> instance each time
  32. * it is invoked.
  33. * The <code>isOpen</code> method will return true on the returned instance.
  34. * @param map properties for entity manager
  35. * @return entity manager instance
  36. * @throws IllegalStateException if the entity manager factory
  37. * has been closed
  38. */
  39. public EntityManager createEntityManager(Map map);
  40. /**
  41. * Return an instance of <code>CriteriaBuilder</code> for the creation of
  42. * <code>CriteriaQuery</code> objects.
  43. * @return CriteriaBuilder instance
  44. * @throws IllegalStateException if the entity manager factory
  45. * has been closed
  46. *
  47. * @since Java Persistence 2.0
  48. */
  49. public CriteriaBuilder getCriteriaBuilder();
  50. /**
  51. * Return an instance of <code>Metamodel</code> interface for access to the
  52. * metamodel of the persistence unit.
  53. * @return Metamodel instance
  54. * @throws IllegalStateException if the entity manager factory
  55. * has been closed
  56. *
  57. * @since Java Persistence 2.0
  58. */
  59. public Metamodel getMetamodel();
  60. /**
  61. * Indicates whether the factory is open. Returns true
  62. * until the factory has been closed.
  63. * @return boolean indicating whether the factory is open
  64. */
  65. public boolean isOpen();
  66. /**
  67. * Close the factory, releasing any resources that it holds.
  68. * After a factory instance has been closed, all methods invoked
  69. * on it will throw the <code>IllegalStateException</code>, except
  70. * for <code>isOpen</code>, which will return false. Once an
  71. * <code>EntityManagerFactory</code> has been closed, all its
  72. * entity managers are considered to be in the closed state.
  73. * @throws IllegalStateException if the entity manager factory
  74. * has been closed
  75. */
  76. public void close();
  77. /**
  78. * Get the properties and associated values that are in effect
  79. * for the entity manager factory. Changing the contents of the
  80. * map does not change the configuration in effect.
  81. * @return properties
  82. * @throws IllegalStateException if the entity manager factory
  83. * has been closed
  84. *
  85. * @since Java Persistence 2.0
  86. */
  87. public Map<String, Object> getProperties();
  88. /**
  89. * Access the cache that is associated with the entity manager
  90. * factory (the "second level cache").
  91. * @return instance of the <code>Cache</code> interface
  92. * @throws IllegalStateException if the entity manager factory
  93. * has been closed
  94. *
  95. * @since Java Persistence 2.0
  96. */
  97. public Cache getCache();
  98. /**
  99. * Return interface providing access to utility methods
  100. * for the persistence unit.
  101. * @return <code>PersistenceUnitUtil</code> interface
  102. * @throws IllegalStateException if the entity manager factory
  103. * has been closed
  104. *
  105. * @since Java Persistence 2.0
  106. */
  107. public PersistenceUnitUtil getPersistenceUnitUtil();
  108. }

  • 独立运行环境下: 
  1. EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
  2. EntityManager entityManger = emf.createEntityManager();

  • 使用容器的环境下,不需要通过Persistence创建EntityManagerFactory和EntityManger,而是通过注解实现 
  1. @PersistenceContext
  2. private EntityManger entityManger
  3. @PersistenceUnit
  4. private EntityMangerFactory emf

  • 实体管理器EntityManager是负责管理Entity的对象。对Entity的操作包括添加、删除、修改和查询,都是通过实体管理器来实现的。


依赖注入EntityManager


  • 在EJB容器 中,EntityManager的获得可以通过标注,使用依赖注入来创建EntityManager实例,代码如下所示。

  1. Public class sessionbean1{
  2. @PersistenceContext
  3. EntityManager em;
  4. 。。。
  5. }
  •    通过将@PersistContext注解标注在EntityManager类型的字段上,这样得到的EntityManager就是容器管理的EntityManager,我们不需要也不应该显式的关闭注入的EntityManager实例。 
  • @PersistenceContex 表示标注的属性entityManager是一个实体管理器EntityManager对象,EJB容器会根据unitName的值来初始化 EntityManager。
  • 注意:如果persistence.xml文件中配置了多个<persistence-unit>。
  • 那么在注入EntityManager对 象时必须指定持久化名称,通过@PersistenceContext注释的unitName属性进行指定,例:
  1. @PersistenceContext(unitName="foshanshop")
  2. EntityManager em;
  • 如果只有一个<persistence-unit>,不需要明确指定。
  • 请注意:Entity Bean被EntityManager管理时,EntityManager会跟踪他的状态改变在任何决定更新实体Bean的时候便会把发生改变的值同步 到数据库中(跟hibernate一样)。
  • 但是如果entity Bean从EntityManager分离后,他是不受管理的,EntityManager无法跟踪他的任何状态改变。


容器管理的EntityManager类型

  1. 容器管理的EntityManager细分为两种类型:事务类型和扩展类型。
  2. 如果@PersistContext未指定type属性,或者指定为PersistContextType.TRANSACTION,则表示该类型是事务类型的。
  3. 如果指定为PersistContextType.EXTENDED,表示该EntityManager是扩展类型的。

  • 事务类型:事务类型的EntityManger是无状态的,可用在无状态会话bean和有状态会话bean。
  • 事务类型的EntityManger依赖于JTA,每次调用EntityManager实例的方法时,EntityManager会查看是否某个持久化上下文与当前事务关联,
  • 如果有,则使用该持久化上下文,如果没有,EntityManager会创建一个持久化上下文,并将该持久化上下文与当前事务关联,事务结束时持久化上下文消失。
    1. JPA支持两种类型的事务:本地资源事务(RESOURCE_LOCAL)和JAVA事务APIJTA
    2. 本地资源事务(RESOUCE_LOCAL):使用JDBC驱动管理的本地事务。
    3. JAVA事务APIJTA):可用于管理分布式事务,管理多数据源的情况。
    4. 容器管理的EntityManager总是使用JTA事务,应用程序管理的EntityManager可以使用本地资源事务,也可以使用JTA事务:
    5. j2SE环境下默认是本地资源事务,在J2EE环境下,默认是JTA。事务的类型在persistence.xml中定义。

  • 扩展类型:扩展类型的EntityManager只能用于有状态会话Bean。
  • 扩展的EntityManager在有状态会话bean实例创建时候创建一个持久化上下文,直到该有状态会话bean销毁时,相应的持久化上下文才被移除。
  • 在扩展的EntityManager中,每次方法调用使用的都是相同的持久化上下文,所以前一次方法调用产生的受管实体下一个方法访问时仍然是受管实体。 

参考来源: http://jpa.group.iteye.com/group/topic/34229
阅读全文
1 0