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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
default TransactionControl
beginTransaction
(boolean rollbackOnly) Start a new transaction.compileQuery
(String query) <T> T
createCountQueryBuilder
(Class<? extends Entity> entityClass) Create a newCountQueryBuilder
which executes count queries.createCountQueryBuilder
(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newCountQueryBuilder
which executes count queries.createCountQueryBuilder
(String modelName) Create a newCountQueryBuilder
which executes count queries.createCountQueryBuilder
(String modelName, Map<String, ?> hints) Create a newCountQueryBuilder
which 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 newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.createPrimaryKeyQueryBuilder
(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.createPrimaryKeyQueryBuilder
(String modelName) Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.createPrimaryKeyQueryBuilder
(String modelName, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.<T> SinglePathQueryBuilder
<T> createQueryBuilder
(Class<? extends Entity> entityClass, Class<T> resultType) Create a newSinglePathQueryBuilder
that returns a single path of the root entity.<T> SinglePathQueryBuilder
<T> Create a newSinglePathQueryBuilder
that returns a single path of the root entity.<T extends Entity>
EntityQueryBuilder<T> createQueryBuilder
(Class<T> entityClass) Create a newEntityQueryBuilder
that returnsEntity
instances of the given type.<T extends Entity>
EntityQueryBuilder<T> createQueryBuilder
(Class<T> entityClass, Map<String, ?> hints) Create a newEntityQueryBuilder
that returnsEntity
instances of the given type.createQueryBuilder
(String modelName) Create a newEntityQueryBuilder
that returnsEntity
instances of the given type.<T> SinglePathQueryBuilder
<T> createQueryBuilder
(String modelName, Class<T> resultType) Create a newSinglePathQueryBuilder
that returns a single path of the root entity.<T> SinglePathQueryBuilder
<T> createQueryBuilder
(String modelName, Class<T> resultType, Map<String, ?> hints) Create a newSinglePathQueryBuilder
that returns a single path of the root entity.createQueryBuilder
(String modelName, Map<String, ?> hints) Create a newEntityQueryBuilder
that returnsEntity
instances of the given type.<T> PathQueryBuilder
<T> createQueryBuilderForPaths
(Class<? extends Entity> entityClass, Class<T> targetClass) Create a newPathQueryBuilder
that 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 newPathQueryBuilder
that returns anObject
[] containing multiple paths of the root entity.<T> PathQueryBuilder
<T> createQueryBuilderForPaths
(String modelName, Class<T> targetClass) Create a newPathQueryBuilder
that returns anObject
[] containing multiple paths of the root entity.<T> PathQueryBuilder
<T> createQueryBuilderForPaths
(String modelName, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilder
that returns anObject
[] containing multiple paths of the root entity.void
doInNewSession
(Runnable runnable) Same as returnFromNewSession but without a return typevoid
doInTransaction
(CheckedConsumer<TransactionControl> consumer) <T> T
doWithConnection
(org.hibernate.jdbc.ReturningWork<T> work) Execute an instance ofReturningWork
using 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 TransactionControl
getIfPresent
(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> T
returnFromNewSession
(Supplier<T> supplier) Run the given supplier with a new session<T> T
returnFromTransaction
(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 aConsumer
instead of aFunction
and 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
TransactionControl
for the newly created transaction - See Also:
-
beginTransaction
- See Also:
-
getCurrentTransaction
- Returns:
- a
TransactionControl
for 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 ofReturningWork
using 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 newEntityQueryBuilder
that returnsEntity
instances of the given type.- Parameters:
entityClass
- the root entity (which is also the return type)
-
createQueryBuilder
Create a newEntityQueryBuilder
that returnsEntity
instances 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 newEntityQueryBuilder
that returnsEntity
instances of the given type.- Parameters:
entityClass
- the root entity (which is also the return type)hints
- optional hints about the query (seeQueryHints
for available hints)
-
createQueryBuilder
Create a newEntityQueryBuilder
that returnsEntity
instances of the given type.- Parameters:
modelName
- the model name of the root entityhints
- optional hints about the query (seeQueryHints
for available hints)
-
createQueryBuilder
<T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType) Create a newSinglePathQueryBuilder
that returns a single path of the root entity.- Parameters:
entityClass
- the root entityresultType
- the type of the path to be returned
-
createQueryBuilder
Create a newSinglePathQueryBuilder
that 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 newSinglePathQueryBuilder
that 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 (seeQueryHints
for available hints)
-
createQueryBuilder
<T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType, Map<String, ?> hints) Create a newSinglePathQueryBuilder
that 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 (seeQueryHints
for available hints)
-
createQueryBuilderForPaths
<T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass) Create a newPathQueryBuilder
that returns anObject
[] containing multiple paths of the root entity. -
createQueryBuilderForPaths
Create a newPathQueryBuilder
that 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 newPathQueryBuilder
that returns anObject
[] containing multiple paths of the root entity.- Parameters:
entityClass
- the root entitytargetClass
- the return type of the query builder (for exampleMap
orObject
[]hints
- optional hints about the query (seeQueryHints
for available hints)
-
createQueryBuilderForPaths
<T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass, Map<String, ?> hints) Create a newPathQueryBuilder
that 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 exampleMap
orObject
[]hints
- optional hints about the query (seeQueryHints
for available hints)
-
createCountQueryBuilder
Create a newCountQueryBuilder
which executes count queries.- Parameters:
entityClass
- the root entity
-
createCountQueryBuilder
Create a newCountQueryBuilder
which executes count queries.- Parameters:
modelName
- the model name of the root entity
-
createCountQueryBuilder
Create a newCountQueryBuilder
which executes count queries.- Parameters:
entityClass
- the root entityhints
- optional hints about the query (seeQueryHints
for available hints)
-
createCountQueryBuilder
Create a newCountQueryBuilder
which executes count queries.- Parameters:
modelName
- the model name of the root entityhints
- optional hints about the query (seeQueryHints
for available hints)
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.- Parameters:
entityClass
- the root entity
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.- Parameters:
modelName
- the model name of the root entity
-
createPrimaryKeyQueryBuilder
PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass, Map<String, ?> hints) Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.- Parameters:
entityClass
- the root entityhints
- optional hints about the query (seeQueryHints
for available hints)
-
createPrimaryKeyQueryBuilder
Create a newPrimaryKeyQueryBuilder
which executes queries forPrimaryKey
instances.- Parameters:
modelName
- the model name of the root entityhints
- optional hints about the query (seeQueryHints
for 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
-