Class CachingLocalEntityProvider<T>
- All Implemented Interfaces:
CachingEntityProvider<T>,EntityProvider<T>,Serializable
LocalEntityProvider that also implements the
CachingEntityProvider interface.
This provider can be used in applications in the same manner as
LocalEntityProvider, with a few exceptions. By default the cache is
on. The cache can be turned off using setCacheEnabled(boolean), in
which case the provider effectively works as a LocalEntityProvider.
If you are going to edit the entities returned by the container, you should
check the setCloneCachedEntities(boolean) before continuing.
- Since:
- 1.0
- Author:
- Petter Holmström (Vaadin Ltd)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCachingLocalEntityProvider(Class<T> entityClass) Creates a newCachingLocalEntityProvider.CachingLocalEntityProvider(Class<T> entityClass, jakarta.persistence.EntityManager entityManager) Creates a newCachingLocalEntityProvider. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsEntity(EntityContainer<T> container, Object entityId, Container.Filter filter) Checks if the persistence storage contains an entity identified byentityIdthat is also matched byfilter.voidflush()Flushes the cache, forcing all entities to be loaded from the persistence storage upon next request.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.intGets the maximum number of entity instances to store in the cache.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.getFirstEntityIdentifier(EntityContainer<T> container, Container.Filter filter, List<SortBy> sortBy) Gets the identifier of the first item in the list of entities determined byfilterandsortBy.getLastEntityIdentifier(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.booleanReturns whether the entity provider currently has the internal cache enabled.booleanReturns whether entities found in the cache should be cloned before they are returned or not.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.voidrefresh()Clears all caches and refreshes any loaded that cannot be discarded entities.refreshEntity(T entity) Refreshes an entity from DB.voidsetCacheEnabled(boolean cacheInUse) Turns the cache on or off.voidsetCloneCachedEntities(boolean clone) Changes the value of theCachingEntityProvider.isCloneCachedEntities()flag.voidsetEntityCacheMaxSize(int maxSize) Sets the maximum number of entity instances to store in the cache.booleanReturns whether the entity provider is currently using the internal cache, which will be the case if both the caching is enabled (CachingEntityProvider.setCacheEnabled(boolean)and there is no filter modifiyingQueryModifierDelegatein use.Methods inherited from class com.vaadin.featurepack.data.jpacontainer.provider.LocalEntityProvider
addPrimaryKeyToSortList, createFilteredQuery, createSiblingQuery, createUnsortedFilteredQuery, detachEntity, doContainsEntity, doGetAllEntityIdentifiers, doGetEntity, doGetEntityCount, doGetEntityIdentifierAt, doGetEntityManager, doGetFirstEntityIdentifier, doGetLastEntityIdentifier, doGetNextEntityIdentifier, doGetPreviousEntityIdentifier, getEntityClassMetadata, getEntityManager, getEntityManagerProvider, getIdentifier, getLazyLoadingDelegate, getQueryModifierDelegate, getSibling, readResolve, setEntitiesDetached, setEntityManager, setEntityManagerProvider, setLazyLoadingDelegate, setQueryModifierDelegate, translateSortBy, writeReplaceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.featurepack.data.jpacontainer.CachingEntityProvider
setEntitiesDetachedMethods inherited from interface com.vaadin.featurepack.data.jpacontainer.EntityProvider
getEntityManager, getEntityManagerProvider, getIdentifier, getLazyLoadingDelegate, getQueryModifierDelegate, setEntityManager, setEntityManagerProvider, setLazyLoadingDelegate, setQueryModifierDelegate
-
Constructor Details
-
CachingLocalEntityProvider
Creates a newCachingLocalEntityProvider. The entity manager must be set usingLocalEntityProvider.setEntityManager(javax.persistence.EntityManager).- Parameters:
entityClass- the entity class (must not be null).
-
CachingLocalEntityProvider
public CachingLocalEntityProvider(Class<T> entityClass, jakarta.persistence.EntityManager entityManager) Creates a newCachingLocalEntityProvider.- Parameters:
entityClass- the entity class (must not be null).entityManager- the entity manager to use (must not be null).
-
-
Method Details
-
flush
public void flush()Description copied from interface:CachingEntityProviderFlushes the cache, forcing all entities to be loaded from the persistence storage upon next request. This feature should be implemented by all caching entity providers.- Specified by:
flushin interfaceCachingEntityProvider<T>
-
getEntityCacheMaxSize
public int getEntityCacheMaxSize()Description copied from interface:CachingEntityProviderGets the maximum number of entity instances to store in the cache. The default value is implementation specific.- Specified by:
getEntityCacheMaxSizein interfaceCachingEntityProvider<T>- Returns:
- the max size, or -1 for unlimited size.
-
isCacheEnabled
public boolean isCacheEnabled()Description copied from interface:CachingEntityProviderReturns whether the entity provider currently has the internal cache enabled. By default, caching should be enabled.
NOTE! If aQueryModifierDelegateis in use and it modifies the filters through theQueryModifierDelegate.filtersWillBeAdded(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.CriteriaQuery, java.util.List)method, caching will NOT be enabled.- Specified by:
isCacheEnabledin interfaceCachingEntityProvider<T>- Returns:
- true if the cache is in use, false otherwise.
-
setCacheEnabled
public void setCacheEnabled(boolean cacheInUse) Description copied from interface:CachingEntityProviderTurns the cache on or off. When the cache is turned off, it is automatically flushed.
NOTE! If aQueryModifierDelegateis in use and it modifies the filters through theQueryModifierDelegate.filtersWillBeAdded(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.CriteriaQuery, java.util.List)method, caching will NOT be enabled.- Specified by:
setCacheEnabledin interfaceCachingEntityProvider<T>- Parameters:
cacheInUse- true to turn the cache on, false to turn it off.
-
usesCache
public boolean usesCache()Description copied from interface:CachingEntityProviderReturns whether the entity provider is currently using the internal cache, which will be the case if both the caching is enabled (CachingEntityProvider.setCacheEnabled(boolean)and there is no filter modifiyingQueryModifierDelegatein use.- Specified by:
usesCachein interfaceCachingEntityProvider<T>- Returns:
- true if the cache is actually in use, false otherwise.
-
setEntityCacheMaxSize
public void setEntityCacheMaxSize(int maxSize) Description copied from interface:CachingEntityProviderSets the maximum number of entity instances to store in the cache. The implementation may decide what to do when the cache is full, but a full cache may never cause an exception. This feature is optional.- Specified by:
setEntityCacheMaxSizein interfaceCachingEntityProvider<T>- Parameters:
maxSize- the new maximum size, or -1 for unlimited size.
-
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>- Overrides:
containsEntityin classLocalEntityProvider<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.
-
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>- Overrides:
getAllEntityIdentifiersin classLocalEntityProvider<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).
-
getEntity
Description copied from interface:EntityProviderLoads the entity identified byentityIdfrom the persistence storage.- Specified by:
getEntityin interfaceEntityProvider<T>- Overrides:
getEntityin classLocalEntityProvider<T>- Parameters:
entityId- the entity identifier (must not be null).- Returns:
- the entity, or null if not found.
-
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 interfaceCachingEntityProvider<T>- Specified by:
isEntitiesDetachedin interfaceEntityProvider<T>- Overrides:
isEntitiesDetachedin classLocalEntityProvider<T>- Returns:
- true if the entities are explicitly detached, false otherwise.
- See Also:
-
isCloneCachedEntities
public boolean isCloneCachedEntities()Description copied from interface:CachingEntityProviderReturns whether entities found in the cache should be cloned before they are returned or not. If this flag is false, two subsequent calls towith the same entity ID and without flushing the cache in between may return the same entity instance. This could be a problem if the instance is modified, as the cache would then contain the locally modified entity instance and not the one that was fetched from the persistence storage.invalid reference
#getEntity(java.lang.Object)If the entity instances are serialized and deserialized before they reach the container, or the container is read-only, entities need not be cloned.
It is undefined what happens if this flag is true and the entities are not cloneable.
The default value of this flag is implementation dependent.
- Specified by:
isCloneCachedEntitiesin interfaceCachingEntityProvider<T>- Returns:
- true if cached entities should be cloned before they are returned, false to return them directly.
- See Also:
-
setCloneCachedEntities
Description copied from interface:CachingEntityProviderChanges the value of theCachingEntityProvider.isCloneCachedEntities()flag.- Specified by:
setCloneCachedEntitiesin interfaceCachingEntityProvider<T>- Parameters:
clone- true to clone cached entities before returning them, false to return them directly.- Throws:
UnsupportedOperationException- if the implementation does not support changing the state of this flag.
-
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>- Overrides:
getEntityCountin classLocalEntityProvider<T>- Parameters:
filter- the filter that should be used to filter the entities (may be null).- Returns:
- the number of matches.
-
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>- Overrides:
getEntityIdentifierAtin classLocalEntityProvider<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.
-
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>- Overrides:
getFirstEntityIdentifierin classLocalEntityProvider<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.
-
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>- Overrides:
getLastEntityIdentifierin classLocalEntityProvider<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.
-
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>- Overrides:
getNextEntityIdentifierin classLocalEntityProvider<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.
-
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>- Overrides:
getPreviousEntityIdentifierin classLocalEntityProvider<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.
-
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>- Overrides:
refreshin classLocalEntityProvider<T>
-
refreshEntity
Description copied from interface:EntityProviderRefreshes an entity from DB. If entity no more exists, null is returned.- Specified by:
refreshEntityin interfaceEntityProvider<T>- Overrides:
refreshEntityin classLocalEntityProvider<T>- Parameters:
entity-- Returns:
- the refreshed entity or null
-