Class JPA
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionPrepare aQueryfor the given model class.static <T> TcallInTransaction(Supplier<T> task) static voidclear()Clear the persistence context, causing all managed entities to become detached.static <T extends Model>
Tcopy(T bean, boolean deep) Create a duplicate copy of the given bean instance.
In case of deep copy, one-to-many records are duplicated.static <T extends Model>
TEdit an instance of the given model class using the given values.
This is a convenient method to reconstruct model object from a key value map, for example HTTP params.static jakarta.persistence.EntityManagerem()Get an instance ofEntityManager.static intExecute a JPQL update query.Return aPropertyof the given model class.Return all the properties of the given model class.static <T extends Model>
TFinds an entity by its primary key.Finds an entity by its primary key.Find multiple entities by their primary key.static voidflush()Synchronize the persistence context to the underlying database.static <T extends Model>
TgetReferenceById(Class<T> modelClass, Long id) Retrieves a reference (proxy) to an entity instance with the specified ID without immediately loading its state from the database.static voidjdbcWork(JPA.JDBCWork work) Perform JDBC related work using theConnectionmanaged by the currentEntityManager.static <T extends Model>
Tmanage(T bean) A convenient method to persist reconstructed unmanaged objects.
This method takes care of relational fields by inspecting the managed state of the referenced objects and also sets reverse lookup fields annotated withOneToMany.mappedBy()annotation.static <T extends Model>
Tmerge(T entity) Merge the state of the given entity into the current persistence context.static Class<?> Return the model class for the given name.models()Return all the non-abstract models found in all the activated modules.static <T extends Model>
Tpersist(T entity) Make an entity managed and persistent.static <T extends Model>
voidrefresh(T entity) Refresh the state of the instance from the database, overwriting changes made to the entity, if any.static <T extends Model>
voidremove(T entity) Remove the entity instance.static voidrunInTransaction(Runnable task) Run the giventaskinside a transaction that is committed after the task is completed.static <T extends Model>
Tsave(T entity) Save the state of the entity.
It uses eitherpersist(Model)ormerge(Model)and callsflush()to synchronize values with database.static voidVerify the values against the database values to ensure the records involved are not modified.
-
Method Details
-
em
public static jakarta.persistence.EntityManager em()Get an instance ofEntityManager. -
execute
Execute a JPQL update query.- Parameters:
query- JPQL query
-
all
Prepare aQueryfor the given model class.- Parameters:
klass- the model class
-
find
Finds an entity by its primary key.- Type Parameters:
T- the type of the model class- Parameters:
klass- the class of the entity to findid- the primary key of the entity to find- Returns:
- entity found by the given id, null otherwise
- See Also:
-
findById
Finds an entity by its primary key.- Type Parameters:
T- the type of the model class- Parameters:
klass- the class of the entity to findid- the primary key of the entity to find- Returns:
- an
Optionalcontaining the found entity, orOptional#empty()if not found
-
findByIds
Find multiple entities by their primary key.Ensure to check the first-level cache first, then the second-level cache. If the entity is found and already managed by the Hibernate Session, the cached entity will be added to the returned List. Therefore, skipping it from being fetched via the multi-load query.
WARNING: The list may contain NULL elements if an id was not found. The list size and order will match the input ids.
- Parameters:
klass- The model classids- The ids to load- Returns:
- list of all the matched records
- See Also:
-
getReferenceById
Retrieves a reference (proxy) to an entity instance with the specified ID without immediately loading its state from the database.This method delegates to
EntityManager.getReference(Class, Object). It is designed for performance optimization, particularly when you need to associate an entity (set a foreign key) without the overhead of a database SELECT query.Note: The returned object is likely a dynamic proxy. The database will only be accessed when you invoke a method on the proxy (other than getting the ID). If the entity does not exist in the database, an
EntityNotFoundExceptionwill be thrown at the time of that access, not at the time of calling this method.- Type Parameters:
T- the type of the model class- Parameters:
modelClass- the class of the entity to retrieveid- the primary key of the entity- Returns:
- a managed entity proxy instance with the state lazily fetched
- Throws:
jakarta.persistence.EntityNotFoundException- if the entity state is accessed, and the entity does not exist in the database- See Also:
-
persist
Make an entity managed and persistent.- See Also:
-
merge
Merge the state of the given entity into the current persistence context.- See Also:
-
save
Save the state of the entity.
It uses eitherpersist(Model)ormerge(Model)and callsflush()to synchronize values with database.- See Also:
-
remove
Remove the entity instance.- See Also:
-
refresh
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.- See Also:
-
flush
public static void flush()Synchronize the persistence context to the underlying database.- See Also:
-
clear
public static void clear()Clear the persistence context, causing all managed entities to become detached.- See Also:
-
verify
Verify the values against the database values to ensure the records involved are not modified.- Throws:
jakarta.persistence.OptimisticLockException- if version mismatch of any a record is deleted
-
edit
Edit an instance of the given model class using the given values.
This is a convenient method to reconstruct model object from a key value map, for example HTTP params.- Parameters:
klass- a model classvalues- key value map where key represents a field name- Returns:
- a JPA managed object of the given model class
-
manage
A convenient method to persist reconstructed unmanaged objects.
This method takes care of relational fields by inspecting the managed state of the referenced objects and also sets reverse lookup fields annotated withOneToMany.mappedBy()annotation.- Parameters:
bean- model instance- Returns:
- JPA managed model instance
- See Also:
-
models
Return all the non-abstract models found in all the activated modules.- Returns:
- Set of model classes
-
model
Return the model class for the given name.- Parameters:
name- name of the model- Returns:
- model class
-
fields
Return all the properties of the given model class. -
field
Return aPropertyof the given model class.- Parameters:
klass- a model classname- name of the property- Returns:
- property or null if property doesn't exist
-
copy
Create a duplicate copy of the given bean instance.
In case of deep copy, one-to-many records are duplicated. Otherwise, one-to-many records will be skipped.- Parameters:
bean- the bean to copydeep- whether to create a deep copy- Returns:
- a copy of the given bean
-
runInTransaction
Run the giventaskinside a transaction that is committed after the task is completed.- Parameters:
task- the task to run.
-
callInTransaction
-
jdbcWork
Perform JDBC related work using theConnectionmanaged by the currentEntityManager.- Parameters:
work- The work to be performed- Throws:
jakarta.persistence.PersistenceException- Generally indicates wrappedSQLException
-