Interface PersistenceService
- All Known Implementing Classes:
PersistenceServiceImpl
public interface PersistenceService
This is the main service to be used by other modules for database interactions.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoiddefault TransactionControlbeginTransaction(boolean rollbackOnly) Start a new transaction.compileQuery(String query) <T> TcreateCountQueryBuilder(Class<? extends Entity> entityClass) Create a newCountQueryBuilderwhich executes count queries.createCountQueryBuilder(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newCountQueryBuilderwhich executes count queries.createCountQueryBuilder(String modelName) Create a newCountQueryBuilderwhich executes count queries.createCountQueryBuilder(String modelName, Map<String, ?> hints) Create a newCountQueryBuilderwhich executes count queries.createDeleteBuilder(Class<? extends Entity> entityClass) Create a new delete builder for the currentSession.createDeleteBuilder(String modelName) Create a new delete builder for the currentSession.createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass) Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.createPrimaryKeyQueryBuilder(String modelName) Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.createPrimaryKeyQueryBuilder(String modelName, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.<T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.<T> SinglePathQueryBuilder<T> Create a newSinglePathQueryBuilderthat returns a single path of the root entity.<T extends Entity>
EntityQueryBuilder<T> createQueryBuilder(Class<T> entityClass) Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.<T extends Entity>
EntityQueryBuilder<T> createQueryBuilder(Class<T> entityClass, Map<String, ?> hints) Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.createQueryBuilder(String modelName) Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.<T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.<T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType, Map<String, ?> hints) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.createQueryBuilder(String modelName, Map<String, ?> hints) Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.<T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.<T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.<T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.<T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.voiddoInNewSession(Runnable runnable) Same as returnFromNewSession but without a return typevoiddoInTransaction(CheckedConsumer<TransactionControl> consumer) <T> TdoWithConnection(org.hibernate.jdbc.ReturningWork<T> work) Execute an instance ofReturningWorkusing the connection (transaction) of the current session.<T extends Entity>
Tget(Class<T> entityClass, Serializable pk) <T extends Entity>
Tget(String modelName, Serializable id) @Nullable TransactionControlgetIfPresent(Class<T> entityClass, Serializable pk) getIfPresent(Class<T> entityClass, String identifier) getIfPresent(String modelName, Serializable id) getIfPresent(String modelName, String identifier) loadEntityClassByName(String entityName) Utility method to load the dynamically generated entity class by entity name.void<T> TreturnFromNewSession(Supplier<T> supplier) Run the given supplier with a new session<T> TreturnFromTransaction(CheckedFunction<TransactionControl, T> function) Run the given function function inside a transaction.
-
Method Details
-
returnFromTransaction
Run the given function function inside a transaction. If a transaction is already running, the existing transaction is used. Newly created entities will automatically be inserted before committing.- Type Parameters:
T- the return type- Parameters:
function- the function to run- Returns:
- the result of the function
-
doInTransaction
The same as returnFromTransaction, but accepts aConsumerinstead of aFunctionand has therefore no return type.- Parameters:
consumer- the function to run
-
returnFromNewSession
Run the given supplier with a new session- Type Parameters:
T- the return type- Parameters:
supplier- the supplier to run- Returns:
- the result of the supplier
-
doInNewSession
Same as returnFromNewSession but without a return type- Parameters:
runnable- to run in a new session
-
create
- Returns:
- an instance of the given entity interface
-
create
- Returns:
- an instance of the given entity model
-
get
- Returns:
- the entity with the given identifier
- Throws:
NotFoundException- if no such entity exists
-
get
- Parameters:
identifier- the unique_id or string key of an entity- Returns:
- the entity with the given identifier
- Throws:
NotFoundException- if no such entity exists
-
get
- Returns:
- the entity with the given identifier
- Throws:
NotFoundException- if no such entity exists
-
get
- Parameters:
identifier- the unique_id or string key of an entity- Returns:
- the entity with the given identifier
- Throws:
NotFoundException- if no such entity exists
-
getIfPresent
- Returns:
- the entity with the given identifier or an empty optional if it does not exist
-
getIfPresent
- Parameters:
identifier- the unique_id or string key of an entity- Returns:
- the entity with the given identifier or an empty optional if it does not exist
-
getIfPresent
- Returns:
- the entity with the given identifier or an empty optional if it does not exist
-
getIfPresent
- Parameters:
identifier- the unique_id or string key of an entity- Returns:
- the entity with the given identifier or an empty optional if it does not exist
-
queryAll
- Returns:
- all entities of the given class
-
queryAll
- Returns:
- all entities of the given class
-
beginTransaction
Start a new transaction. PreferablydoInTransaction(CheckedConsumer)andreturnFromTransaction(CheckedFunction)should be used. The transaction handling (commit/rollback) must be handled by the user of this method.- Parameters:
rollbackOnly- if the transaction should be rolled back at the end of the transaction- Returns:
- the
TransactionControlfor the newly created transaction - See Also:
-
beginTransaction
- See Also:
-
getCurrentTransaction
- Returns:
- a
TransactionControlfor the currently running transaction (or null)
-
getCurrentSession
PersistenceSession getCurrentSession()- Returns:
- the currently active session - this may open a new thread-bound session.
-
doWithConnection
<T> T doWithConnection(org.hibernate.jdbc.ReturningWork<T> work) Execute an instance ofReturningWorkusing the connection (transaction) of the current session. -
txInvoker
Invoker txInvoker() -
createDeleteBuilder
Create a new delete builder for the currentSession.- Parameters:
entityClass- the root entity (which is also the return type)
-
createDeleteBuilder
Create a new delete builder for the currentSession.- Parameters:
modelName- the model name of the root entity
-
compileQuery
-
createQueryBuilder
Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.- Parameters:
entityClass- the root entity (which is also the return type)
-
createQueryBuilder
Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.- Parameters:
modelName- the model name of the root entity
-
createQueryBuilder
<T extends Entity> EntityQueryBuilder<T> createQueryBuilder(Class<T> entityClass, Map<String, ?> hints) Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.- Parameters:
entityClass- the root entity (which is also the return type)hints- optional hints about the query (seeQueryHintsfor available hints)
-
createQueryBuilder
Create a newEntityQueryBuilderthat returnsEntityinstances of the given type.- Parameters:
modelName- the model name of the root entityhints- optional hints about the query (seeQueryHintsfor available hints)
-
createQueryBuilder
<T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.- Parameters:
entityClass- the root entityresultType- the type of the path to be returned
-
createQueryBuilder
Create a newSinglePathQueryBuilderthat returns a single path of the root entity.- Parameters:
modelName- the model name of the root entityresultType- the type of the path to be returned
-
createQueryBuilder
<T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType, Map<String, ?> hints) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.- Parameters:
entityClass- the root entityresultType- the type of the path to be returnedhints- optional hints about the query (seeQueryHintsfor available hints)
-
createQueryBuilder
<T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType, Map<String, ?> hints) Create a newSinglePathQueryBuilderthat returns a single path of the root entity.- Parameters:
modelName- the model name of the root entityresultType- the type of the path to be returnedhints- optional hints about the query (seeQueryHintsfor available hints)
-
createQueryBuilderForPaths
<T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity. -
createQueryBuilderForPaths
Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity. -
createQueryBuilderForPaths
<T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.- Parameters:
entityClass- the root entitytargetClass- the return type of the query builder (for exampleMaporObject[]hints- optional hints about the query (seeQueryHintsfor available hints)
-
createQueryBuilderForPaths
<T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilderthat returns anObject[] containing multiple paths of the root entity.- Parameters:
modelName- the model name of the root entitytargetClass- the return type of the query builder (for exampleMaporObject[]hints- optional hints about the query (seeQueryHintsfor available hints)
-
createCountQueryBuilder
Create a newCountQueryBuilderwhich executes count queries.- Parameters:
entityClass- the root entity
-
createCountQueryBuilder
Create a newCountQueryBuilderwhich executes count queries.- Parameters:
modelName- the model name of the root entity
-
createCountQueryBuilder
Create a newCountQueryBuilderwhich executes count queries.- Parameters:
entityClass- the root entityhints- optional hints about the query (seeQueryHintsfor available hints)
-
createCountQueryBuilder
Create a newCountQueryBuilderwhich executes count queries.- Parameters:
modelName- the model name of the root entityhints- optional hints about the query (seeQueryHintsfor available hints)
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.- Parameters:
entityClass- the root entity
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.- Parameters:
modelName- the model name of the root entity
-
createPrimaryKeyQueryBuilder
PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.- Parameters:
entityClass- the root entityhints- optional hints about the query (seeQueryHintsfor available hints)
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilderwhich executes queries forPrimaryKeyinstances.- Parameters:
modelName- the model name of the root entityhints- optional hints about the query (seeQueryHintsfor available hints)
-
loadEntityClassByName
Utility method to load the dynamically generated entity class by entity name.- Parameters:
entityName- name of the entity to load. Can be the fully qualified class name or just the entity name- Returns:
- the entity class instance
-
addSessionListener
-
removeSessionListener
-