Class LocalEntityProvider<T>
java.lang.Object
com.vaadin.featurepack.data.jpacontainer.provider.LocalEntityProvider<T>
- All Implemented Interfaces:
EntityProvider<T>,Serializable
- Direct Known Subclasses:
CachingLocalEntityProvider,EntityProvider,MutableLocalEntityProvider
A read-only entity provider that works with a local
EntityManager.
Most important features and limitations:
- Does not do any internal caching, all information is always accessed directly from the EntityManager
- Explicitly detaches entities by default (see
isEntitiesDetached())- Performs a serialize-deserialize cycle to clone entities in order to explicitly detach them from the persistence context (This is ugly!).
- Uses lazy-loading of entities (when using detached entities, references and collections within the entities should be configured to be fetched eagerly, though)
CachingLocalEntityProvider should be used instead. However, this
entity provider consumes less memory than the caching provider.- Since:
- 1.0
- Author:
- Petter Holmström (Vaadin Ltd)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLocalEntityProvider(Class<T> entityClass) Creates a newLocalEntityProvider.LocalEntityProvider(Class<T> entityClass, EntityManagerProvider entityManagerProvider) Creates a newLocalEntityProviderwith the specifiedEntityManagerProvider.LocalEntityProvider(Class<T> entityClass, jakarta.persistence.EntityManager entityManager) Creates a newLocalEntityProvider. -
Method Summary
Modifier and TypeMethodDescriptionaddPrimaryKeyToSortList(List<SortBy> original) Creates a copy oforiginaland adds an entry for the primary key to the end of the list.booleancontainsEntity(EntityContainer<T> container, Object entityId, Container.Filter filter) Checks if the persistence storage contains an entity identified byentityIdthat is also matched byfilter.protected jakarta.persistence.TypedQuery<Object> createFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, Container.Filter filter, List<SortBy> sortBy, boolean swapSortOrder) Creates a filtered, optionally sorted, query.protected jakarta.persistence.TypedQuery<Object> createSiblingQuery(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy, boolean backwards) This method creates a query that can be used to fetch the siblings of a specific entity.protected jakarta.persistence.TypedQuery<Object> createUnsortedFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, Container.Filter filter) Creates a filtered query that does not do any sorting.protected TdetachEntity(T entity) Detachesentityfrom the entity manager.protected booleandoContainsEntity(EntityContainer<T> container, Object entityId, Container.Filter filter) doGetAllEntityIdentifiers(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) protected TdoGetEntity(Object entityId) protected intdoGetEntityCount(EntityContainer<T> container, Container.Filter filter) protected ObjectdoGetEntityIdentifierAt(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy, int index) protected jakarta.persistence.EntityManagerGets the entity manager.protected ObjectdoGetFirstEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) protected ObjectdoGetLastEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) protected ObjectdoGetNextEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) protected ObjectdoGetPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) getAllEntityIdentifiers(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Gets the identifiers of all items that matchfilter.getEntity(EntityContainer<T> container, Object entityId) Loads the entity identified byentityIdfrom the persistence storage.protected EntityClassMetadata<T> Gets the metadata for the entity class.intgetEntityCount(EntityContainer<T> container, Container.Filter filter) Gets the number of entities that are matched byfilter.getEntityIdentifierAt(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy, int index) Gets the identifier of the entity at positionindexin the result set determined fromfilterandsortBy.jakarta.persistence.EntityManagerGets the entity manager.Gets theEntityManagerProviderthat is used to find the current entity manager unless one is specified usingsetEntityManager(javax.persistence.EntityManager).getFirstEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Gets the identifier of the first item in the list of entities determined byfilterandsortBy.getIdentifier(T entity) Returns identifier for given entitygetLastEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Gets the identifier of the last item in the list of entities determined byfilterandsortBy.getNextEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) Gets the identifier of the item next to the item identified byentityIdin the list of entities determined byfilterandsortBy.getPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) Gets the identifier of the item previous to the item identified byentityIdin the list of entities determined byfilterandsortBy.protected ObjectgetSibling(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy, boolean backwards) Ifbackwardsis false, this method will return the identifier of the entity next to the entity identified byentityId.booleanIf this method returns true, all entities returned from this entity provider are explicitly detached from the persistence context before returned, regardless of whether the persistence context is extended or transaction-scoped.protected Objectvoidrefresh()Clears all caches and refreshes any loaded that cannot be discarded entities.refreshEntity(T entity) Refreshes an entity from DB.voidsetEntitiesDetached(boolean detached) Specifies whether the entities returned by the entity provider should be explicitly detached or not.voidsetEntityManager(jakarta.persistence.EntityManager entityManager) Sets the entity manager.voidsetEntityManagerProvider(EntityManagerProvider entityManagerProvider) Sets theEntityManagerProviderthat is used to find the current entity manager unless set usingsetEntityManager(javax.persistence.EntityManager)voidsetLazyLoadingDelegate(LazyLoadingDelegate delegate) Set the delegate used for lazy loading.voidSets theQueryModifierDelegate, which is called in the different stages that the EntityProvider builds a criteria query.protected jakarta.persistence.criteria.OrdertranslateSortBy(SortBy sortBy, boolean swapSortOrder, jakarta.persistence.criteria.CriteriaBuilder cb, jakarta.persistence.criteria.Root<T> root) Translates SortBy instances, which possibly contain nested properties (e.g. name.firstName, name.lastName) into Order instances which can be used in a CriteriaQuery.protected Object
-
Constructor Details
-
LocalEntityProvider
Creates a newLocalEntityProvider.- Parameters:
entityClass- the entity class (must not be null).entityManager- the entity manager to use (must not be null).
-
LocalEntityProvider
Creates a newLocalEntityProvider. The entity manager or an entity manager provider must be set usingsetEntityManager(javax.persistence.EntityManager)orsetEntityManagerProvider(EntityManagerProvider)respectively.- Parameters:
entityClass- the entity class (must not be null).
-
LocalEntityProvider
Creates a newLocalEntityProviderwith the specifiedEntityManagerProvider.- Parameters:
entityClass-entityManagerProvider-
-
-
Method Details
-
writeReplace
- Throws:
ObjectStreamException
-
readResolve
- Throws:
ObjectStreamException
-
setEntityManagerProvider
Sets theEntityManagerProviderthat is used to find the current entity manager unless set usingsetEntityManager(javax.persistence.EntityManager)- Specified by:
setEntityManagerProviderin interfaceEntityProvider<T>- Parameters:
entityManagerProvider- The entity manager provider to set.
-
getEntityManagerProvider
Gets theEntityManagerProviderthat is used to find the current entity manager unless one is specified usingsetEntityManager(javax.persistence.EntityManager).- Specified by:
getEntityManagerProviderin interfaceEntityProvider<T>- Returns:
- the entity manager provider,
-
setEntityManager
public void setEntityManager(jakarta.persistence.EntityManager entityManager) Sets the entity manager.- Specified by:
setEntityManagerin interfaceEntityProvider<T>- Parameters:
entityManager- the entity manager to set.
-
getEntityClassMetadata
Gets the metadata for the entity class.- Returns:
- the metadata (never null).
-
getEntityManager
public jakarta.persistence.EntityManager getEntityManager()Gets the entity manager. If no entity manager has been set, the one returned by the registered entity manager provider is returned.- Specified by:
getEntityManagerin interfaceEntityProvider<T>- Returns:
- the entity manager.
-
doGetEntityManager
Gets the entity manager.- Returns:
- the entity manager (never null).
- Throws:
IllegalStateException- if no entity manager is set.
-
addPrimaryKeyToSortList
Creates a copy oforiginaland adds an entry for the primary key to the end of the list.- Parameters:
original- the original list of sorting instructions (must not be null, but may be empty).- Returns:
- a new list with the added entry for the primary key.
-
translateSortBy
protected jakarta.persistence.criteria.Order translateSortBy(SortBy sortBy, boolean swapSortOrder, jakarta.persistence.criteria.CriteriaBuilder cb, jakarta.persistence.criteria.Root<T> root) Translates SortBy instances, which possibly contain nested properties (e.g. name.firstName, name.lastName) into Order instances which can be used in a CriteriaQuery.- Parameters:
sortBy- the SortBy instance to translateswapSortOrder- swaps the specified sort order if true.cb- theCriteriaBuilderto useroot- theCriteriaQueryRootto be used.- Returns:
-
createUnsortedFilteredQuery
protected jakarta.persistence.TypedQuery<Object> createUnsortedFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, Container.Filter filter) Creates a filtered query that does not do any sorting.- Parameters:
fieldsToSelect- the fields to select (must not be null).filter- the filter to apply, or null if no filters should be applied.- Returns:
- the query (never null).
- See Also:
-
createFilteredQuery
protected jakarta.persistence.TypedQuery<Object> createFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, Container.Filter filter, List<SortBy> sortBy, boolean swapSortOrder) Creates a filtered, optionally sorted, query.- Parameters:
fieldsToSelect- the fields to select (must not be null).filter- the filter to apply, or null if no filters should be applied.sortBy- the fields to sort by (must include at least one field), or null if the result should not be sorted at all.swapSortOrder- true to swap the sort order, false to use the sort order specified insortBy. Only applies ifsortByis not null.- Returns:
- the query (never null).
-
doContainsEntity
protected boolean doContainsEntity(EntityContainer<T> container, Object entityId, Container.Filter filter) -
containsEntity
public boolean containsEntity(EntityContainer<T> container, Object entityId, Container.Filter filter) Description copied from interface:EntityProviderChecks if the persistence storage contains an entity identified byentityIdthat is also matched byfilter.- Specified by:
containsEntityin interfaceEntityProvider<T>- Parameters:
entityId- the entity identifier (must not be null).filter- the filter that the entity should match (may be null).- Returns:
- true if the entity exists, false if not.
-
doGetEntity
-
getEntity
Description copied from interface:EntityProviderLoads the entity identified byentityIdfrom the persistence storage.- Specified by:
getEntityin interfaceEntityProvider<T>- Parameters:
entityId- the entity identifier (must not be null).- Returns:
- the entity, or null if not found.
-
doGetEntityIdentifierAt
protected Object doGetEntityIdentifierAt(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy, int index) -
getEntityIdentifierAt
public Object getEntityIdentifierAt(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy, int index) Description copied from interface:EntityProviderGets the identifier of the entity at positionindexin the result set determined fromfilterandsortBy.- Specified by:
getEntityIdentifierAtin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).index- the index of the entity to fetch.- Returns:
- the entity identifier, or null if not found.
-
doGetEntityCount
-
getEntityCount
Description copied from interface:EntityProviderGets the number of entities that are matched byfilter. If no filter has been specified, the total number of entities is returned.- Specified by:
getEntityCountin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).- Returns:
- the number of matches.
-
doGetFirstEntityIdentifier
protected Object doGetFirstEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) -
getFirstEntityIdentifier
public Object getFirstEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Description copied from interface:EntityProviderGets the identifier of the first item in the list of entities determined byfilterandsortBy.- Specified by:
getFirstEntityIdentifierin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).- Returns:
- the identifier of the first entity, or null if there are no
entities matching
filter.
-
doGetLastEntityIdentifier
protected Object doGetLastEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) -
getLastEntityIdentifier
public Object getLastEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Description copied from interface:EntityProviderGets the identifier of the last item in the list of entities determined byfilterandsortBy.- Specified by:
getLastEntityIdentifierin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).- Returns:
- the identifier of the last entity, or null if there are no
entities matching
filter.
-
getSibling
protected Object getSibling(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy, boolean backwards) Ifbackwardsis false, this method will return the identifier of the entity next to the entity identified byentityId. If true, this method will return the identifier of the entity previous to the entity identified byentityId.filterandsortByis used to define and limit the list of entities to be used for determining the sibling.- Parameters:
entityId- the identifier of the entity whose sibling to retrieve (must not be null).filter- an optional filter to limit the entities (may be null).sortBy- the order in which the list should be sorted (must not be null).backwards- true to fetch the previous sibling, false to fetch the next sibling.- Returns:
- the identifier of the "sibling".
-
createSiblingQuery
protected jakarta.persistence.TypedQuery<Object> createSiblingQuery(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy, boolean backwards) This method creates a query that can be used to fetch the siblings of a specific entity. Ifbackwardsis false, the query will begin with the entity next to the entity identified byentityId. Ifbackwardsis false, the query will begin with the entity prior to the entity identified byentityId.- Parameters:
entityId- the identifier of the entity whose sibling to retrieve (must not be null).filter- an optional filter to limit the entities (may be null).sortBy- the order in which the list should be sorted (must not be null).backwards- true to fetch the previous sibling, false to fetch the next sibling.- Returns:
- the query that will return the sibling and all the subsequent entities unless limited.
-
doGetNextEntityIdentifier
protected Object doGetNextEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) -
getNextEntityIdentifier
public Object getNextEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) Description copied from interface:EntityProviderGets the identifier of the item next to the item identified byentityIdin the list of entities determined byfilterandsortBy.- Specified by:
getNextEntityIdentifierin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).- Returns:
- the identifier of the next entity, or null if there are no
entities matching
filterorentityIdis the last item.
-
doGetPreviousEntityIdentifier
protected Object doGetPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) -
getPreviousEntityIdentifier
public Object getPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, Container.Filter filter, List<SortBy> sortBy) Description copied from interface:EntityProviderGets the identifier of the item previous to the item identified byentityIdin the list of entities determined byfilterandsortBy.- Specified by:
getPreviousEntityIdentifierin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).- Returns:
- the identifier of the previous entity, or null if there are no
entities matching
filterorentityIdis the first item.
-
detachEntity
Detachesentityfrom the entity manager. Ifentityis null, then null is returned. IfisEntitiesDetached()is false,entityis returned directly.- Parameters:
entity- the entity to detach.- Returns:
- the detached entity.
-
isEntitiesDetached
public boolean isEntitiesDetached()Description copied from interface:EntityProviderIf this method returns true, all entities returned from this entity provider are explicitly detached from the persistence context before returned, regardless of whether the persistence context is extended or transaction-scoped. Thus, no lazy-loaded associations will work and any changes made to the entities will not be reflected in the persistence context unless the entity is merged.If this method returns false, the entities returned may be managed or detached, depending on the scope of the persistence context.
The default value is implementation specific.
- Specified by:
isEntitiesDetachedin interfaceEntityProvider<T>- Returns:
- true if the entities are explicitly detached, false otherwise.
- See Also:
-
setEntitiesDetached
Description copied from interface:EntityProviderSpecifies whether the entities returned by the entity provider should be explicitly detached or not. SeeEntityProvider.isEntitiesDetached()for a more detailed description of the consequences.- Specified by:
setEntitiesDetachedin interfaceEntityProvider<T>- Parameters:
detached- true to request explicitly detached entities, false otherwise.- Throws:
UnsupportedOperationException- if the implementation does not allow the user to change the way entities are returned.
-
doGetAllEntityIdentifiers
protected List<Object> doGetAllEntityIdentifiers(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) -
getAllEntityIdentifiers
public List<Object> getAllEntityIdentifiers(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Description copied from interface:EntityProviderGets the identifiers of all items that matchfilter. This method only exists to speed up, which in turn is used byinvalid reference
com.vaadin.data.Container#getItemIds()and its subclasses (e.g. ComboBox). Using this method is not recommended, as it does not use lazy loading.invalid reference
com.vaadin.ui.AbstractSelect- Specified by:
getAllEntityIdentifiersin interfaceEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).sortBy- the properties to sort by, if any (may be null).- Returns:
- an unmodifiable list of entity identifiers (never null).
-
setQueryModifierDelegate
Description copied from interface:EntityProviderSets theQueryModifierDelegate, which is called in the different stages that the EntityProvider builds a criteria query.- Specified by:
setQueryModifierDelegatein interfaceEntityProvider<T>- Parameters:
delegate- the delegate.
-
getQueryModifierDelegate
- Specified by:
getQueryModifierDelegatein interfaceEntityProvider<T>- Returns:
- the registered
QueryModifierDelegate.
-
getIdentifier
Description copied from interface:EntityProviderReturns identifier for given entity- Specified by:
getIdentifierin interfaceEntityProvider<T>- Parameters:
entity-
-
refreshEntity
Description copied from interface:EntityProviderRefreshes an entity from DB. If entity no more exists, null is returned.- Specified by:
refreshEntityin interfaceEntityProvider<T>- Parameters:
entity-- Returns:
- the refreshed entity or null
-
setLazyLoadingDelegate
Description copied from interface:EntityProviderSet the delegate used for lazy loading.- Specified by:
setLazyLoadingDelegatein interfaceEntityProvider<T>- Parameters:
delegate- theLazyLoadingDelegateto use.
-
getLazyLoadingDelegate
- Specified by:
getLazyLoadingDelegatein interfaceEntityProvider<T>- Returns:
- the
LazyLoadingDelegatein use or null if none registered.
-
refresh
public void refresh()Description copied from interface:EntityProviderClears all caches and refreshes any loaded that cannot be discarded entities.- Specified by:
refreshin interfaceEntityProvider<T>
-