You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jpacontainer-entityprovider.asciidoc 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ---
  2. title: Entity Providers
  3. order: 5
  4. layout: page
  5. ---
  6. [[jpacontainer.entityprovider]]
  7. = Entity Providers
  8. Entity providers provide access to entities persisted in a data store. They are
  9. essentially wrappers over a JPA entity manager, adding optimizations and other
  10. features important when binding persistent data to a user interface.
  11. The choice and use of entity providers is largely invisible if you create your
  12. [classname]#JPAContainer# instances with the [classname]#JPAContainerFactory#,
  13. which hides such details.
  14. JPAContainer entity providers can be customized, which is necessary for some
  15. purposes. Entity providers can be Enterprise JavaBeans (EJBs), which is useful
  16. when you use them in a Java EE application server.
  17. [[jpacontainer.entityprovider.built-in]]
  18. == Built-In Entity Providers
  19. JPAContainer includes various kinds of built-in entity providers: caching and
  20. non-caching, read-write and read-only, and batchable.
  21. __Caching__ is useful for performance, but takes some memory for the cache and
  22. makes the provider stateful. __Batching__, that is, running updates in larger
  23. batches, can also enhance performance and be used together with caching. It is
  24. stateless, but doing updates is a bit more complex than otherwise.
  25. Using a __read-only__ container is preferable if read-write capability is not
  26. needed.
  27. All built-in providers are __local__ in the sense that they provide access to
  28. entities using a local JPA entity manager.
  29. The [classname]#CachingMutableLocalEntityProvider# is usually recommended as the
  30. first choise for read-write access and [classname]#CachingLocalEntityProvider#
  31. for read-only access.
  32. === [classname]#LocalEntityProvider#
  33. A read-only, lazy loading entity provider that does not perform caching and
  34. reads its data directly from an entity manager.
  35. You can create the provider with [methodname]#makeNonCachedReadOnly()# method in
  36. [classname]#JPAContainerFactory#.
  37. === [classname]#MutableLocalEntityProvider#
  38. Extends [classname]#LocalEntityProvider# with write support. All changes are
  39. directly sent to the entity manager.
  40. Transactions can be handled either internally by the provider, which is the
  41. default, or by the container. In the latter case, you can extend the class and
  42. annotate it, for example, as described in
  43. <<jpacontainer.entityprovider.built-in>>.
  44. The provider can notify about updates to entities through the
  45. [interfacename]#EntityProviderChangeNotifier# interface.
  46. === [classname]#BatchableLocalEntityProvider#
  47. A simple non-caching implementation of the
  48. [interfacename]#BatchableEntityProvider# interface. It extends
  49. [classname]#MutableLocalEntityProvider# and simply passes itself to the
  50. [methodname]#batchUpdate()# callback method. This will work properly if the
  51. entities do not contain any references to other entities that are managed by the
  52. same container.
  53. === [classname]#CachingLocalEntityProvider#
  54. A read-only, lazy loading entity provider that caches both entities and query
  55. results for different filter/sortBy combinations. When the cache gets full, the
  56. oldest entries in the cache are removed. The maximum number of entities and
  57. entity IDs to cache for each filter/sortBy combination can be configured in the
  58. provider. The cache can also be manually flushed. When the cache grows full, the
  59. oldest items are removed.
  60. You can create the provider with [methodname]#makeReadOnly()# method in
  61. [classname]#JPAContainerFactory#.
  62. === [classname]#CachingMutableLocalEntityProvider#
  63. Just like [classname]#CachingLocalEntityProvider#, but with read-write access.
  64. For read access, caching works just like in the read-only provider. When an
  65. entity is added or updated, the cache is flushed in order to make sure the added
  66. or updated entity shows up correctly when using filters and/or sorting. When an
  67. entity is removed, only the filter/sortBy-caches that actually contain the item
  68. are flushed.
  69. This is perhaps the most commonly entity provider that you should consider using
  70. for most tasks. You can create it with the [methodname]#make()# method in
  71. [classname]#JPAContainerFactory#.
  72. === [classname]#CachingBatchableLocalEntityProvider#
  73. This provider supports making updates in __batches__. You need to implement a
  74. [interfacename]#BatchUpdateCallback# that does all the updates and execute the
  75. batch by calling [methodname]#batchUpdate()# on the provider.
  76. The provider is an extension of the
  77. [classname]#CachingMutableLocalEntityProvider# that implements the
  78. [interfacename]#BatchableEntityProvider# interface. This will work properly if
  79. the entities do not contain any references to other entities that are managed by
  80. the same container.
  81. You can create the provider with [methodname]#makeBatchable()# method in
  82. [classname]#JPAContainerFactory#.
  83. [[jpacontainer.entityprovider.jndi]]
  84. == Using JNDI Entity Providers in JEE6 Environment
  85. JPAContainer 2.0 introduced a new set of entity providers specifically for
  86. working in a [literal]#++JEE6++# environment. In a JEE environment, you should
  87. use an entity manager provided by the application server and, usually,
  88. [literal]#++JTA++# transactions instead of transactions provided by JPA. Entity
  89. providers in [package]#com.vaadin.addon.jpacontainer.provider.jndijta# package
  90. work mostly the same way as the normal providers discussed earlier, but use JNDI
  91. lookups to get reference to an [interfacename]#EntityManager# and to a JTA
  92. transaction.
  93. The JNDI providers work with almost no special configuration at all. The
  94. [classname]#JPAContainerFactory# has factory methods for creating various JNDI
  95. provider types. The only thing that you commonly need to do is to expose the
  96. [interfacename]#EntityManager# to a JNDI address. By default, the JNDI providers
  97. look for the [interfacename]#EntityManager# from "
  98. java:comp/env/persistence/em". This can be done with the following snippet in
  99. [filename]#web.xml# or with similar configuration with annotations.
  100. ----
  101. <persistence-context-ref>
  102. <persistence-context-ref-name>
  103. persistence/em
  104. </persistence-context-ref-name>
  105. <persistence-unit-name>MYPU</persistence-unit-name>
  106. </persistence-context-ref>
  107. ----
  108. The " [literal]#++MYPU++#" is the identifier of your persistence unit defined in
  109. your [filename]#persistence.xml# file.
  110. If you choose to annotate your servlets (instead of using the
  111. [filename]#web.xml# file as described above), you can simply add the following
  112. annotation to your servlet.
  113. ----
  114. @PersistenceContext(name="persistence/em",unitName="MYPU")
  115. ----
  116. If you wish to use another address for the persistence context, you can define
  117. them with the [methodname]#setJndiAddresses()# method. You can also define the
  118. location for the JTA [classname]#UserTransaction#, but that should be always
  119. accessible from " java:comp/UserTransaction" by the JEE6 specification.
  120. [[jpacontainer.entityprovider.ejb]]
  121. == Entity Providers as Enterprise Beans
  122. Entity providers can be Enterprise JavaBeans (EJB). This may be useful if you
  123. use JPAContainer in a Java EE application server. In such case, you need to
  124. implement a custom entity provider that allows the server to inject the entity
  125. manager.
  126. For example, if you need to use Java Transaction API (JTA) for JPA transactions,
  127. you can implement such entity provider as follows. Just extend a built-in entity
  128. provider of your choise and annotate the entity manager member as
  129. [literal]#++@PersistenceContext++#. Entity providers can be either stateless or
  130. stateful session beans. If you extend a caching entity provider, it has to be
  131. stateful.
  132. ----
  133. @Stateless
  134. @TransactionManagement
  135. public class MyEntityProviderBean extends
  136. MutableLocalEntityProvider<MyEntity> {
  137. @PersistenceContext
  138. private EntityManager em;
  139. protected LocalEntityProviderBean() {
  140. super(MyEntity.class);
  141. setTransactionsHandledByProvider(false);
  142. }
  143. @Override
  144. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  145. protected void runInTransaction(Runnable operation) {
  146. super.runInTransaction(operation);
  147. }
  148. @PostConstruct
  149. public void init() {
  150. setEntityManager(em);
  151. /*
  152. * The entity manager is transaction-scoped, which means
  153. * that the entities will be automatically detached when
  154. * the transaction is closed. Therefore, we do not need
  155. * to explicitly detach them.
  156. */
  157. setEntitiesDetached(false);
  158. }
  159. }
  160. ----
  161. If you have more than one EJB provider, you might want to create an abstract
  162. super class of the above and only define the entity type in implementations. You
  163. can implement an entity provider as a managed bean in Spring Framefork the same
  164. way.