Since Vaadin 8 DataProvider is to be used instead of container
Implements Container interface
Supports especially EclipseLink implementation of JPA and Hibernate
JPA:
- EntityManager is the main interface
- JPQL the query language
- or native SQL
- or Criteria API (JPA 2.0)
Apache License 2.0 (free for non-commercial use)
JPAContainer concepts
- EntityProvider interface - data access is handled, different entity providers provided
- by default unbuffered - changes are written immediately when setValue() is called or user edits a bound field
- batchable containers can be set as buffered, changes are written on calling commit()
- item level buffering : when updating item property values
- container level : when adding or deleting items
- OptimisticLockException can be thrown
- documentation
- API documentation
- tutorial
- AddressBook demo
- demo
- download and install, or
- as Maven dependency, or
- create as Maven archetype
- Entity
- implements Serializable interface
- default constructor
- Metadata (by javax.persistence annotation or XML)
- Create JPAContainer
- JPAContainerFactory provides various make...() methods, takes class type of entity class as first parameter, and second class being a persistence unit name (persistence context) or an EntityManager instance
- make() - uses CachingMutableLocalEntityProvider
- makeReadOnly() - CachingLocalEntityProvider
- makeBatchable() - BatchableLocalEntityProvider
- makeNonCached() - MutableLocalEntityProvider
- makeNonCachedReadOnly() - LocalEntityProvider
- obtain EntityManager - JPAContainerFactory.createEntityManagerForPersistenceUnit("book-examples")
- create manually (not normally do this, but when need to use a custom entity provider or extend JPAContainer)
- Creating and accessing entities
- addEntity() returns a new ID; then getItem(itemId)
- Item
- getItem() - gets the Item
- getEntity() - gets the entity object
- states:
- persistent : is it actually persistent, i.e. fetched from a database
- modified (item buffering) : has changed and not yet committed, only when setBuffered(true) for item
- dirty (container buffering) : has been modified after fetched, possible only when buffering enabled for container
- deleted (container buffering) : been marked for deletion
- Refreshing
- if item changed outside container (directly through EntityManager) or changed in database, need to refresh
- refresh() : discards all caches and buffers, refreshes all loaded items, changes discarded
- refreshItem() : refresh single item
- Nested Properties
- can do with one-to-one or many-to-one relationship
- addNestedContainerProperty() with dot-separated path
- Hierarchical Container (JPAContainer implements Container.Hierarchical)
- needs a parent ID column in database
- they are essentially wrappers over JPA entity manager with optimizations and other features for different scenarios
- if uses factory make...() methods, the choice is largely invisible
- built-ins
- overview
- caching : better performance, more memory usage, makes provider stateful
- batching : running updates in larger batches, enhance performance and used with caching
- read-only : preferable when only read
- CachingMutableLocalEntityProvider (make()) : first choice for read-write
- CachingLocalEntityProvider (makeReadOnly) : first choice for read only
- LocalEntityProvider (makeNonCachedReadOnly) - read-only, laze loading, no caching, read directly from entity manager
- MutableLocalEntityProvider - extend LocalEntityProvider with write support; all changes send to entity manager directly
- provider handle transaction - default
- container handle transaction - extend the clas and annotate it
- change event can be listened
- BatchableLocalEntityProvider : non-caching implementation of BatchableEntityProvider
- CachingLocalEntityProvider (makeReadOnly) : read-only, lazy loading, caches entities and query results, cache size can be configured, can be manually flushed
- CachingMutableLocalEntityProvider : like above but writable, when an entity is added or updated, cache is flushed; when entity is removed, affected caches flushed; perhaps most commonly used
- CachingBatchableLocalEntityProvider : supports batch updates; need to implement a BatchUpdateCallback that does all updates, an extension of the above; work properly if entities do not contain any references to other entities that are managed by the same container
- built-ins for JEE environment
- Entity providers in com.vaadin.addon.jpacontainer.provider.jndijta works mostly the same but use JNDI lookups to get EntityManager and JTA transaction
- Entity providers as enterprise (session) beans
- usage
- allow server injection
- use JTA (java transaction API)
- just extend and annotate
- stateless (no caching) or stateful (if extends a caching entity provider)
- see an example
Automatic form generation