Package com.axelor.db
Class JPA
java.lang.Object
com.axelor.db.JPA
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionPrepare aQuery
for the given model class.static <T> T
callInTransaction
(Supplier<T> task) static void
clear()
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.EntityManager
em()
Get an instance ofEntityManager
.static int
Execute a JPQL update query.Return aProperty
of the given model class.Return all the properties of the given model class.static <T extends Model>
TFind by primary key.Find multiple entities by their primary key.
Ensure to check the first-level cache first then the second-level cache and, 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.static void
flush()
Synchronize the persistence context to the underlying database.static void
jdbcWork
(JPA.JDBCWork work) Perform JDBC related work using theConnection
managed 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 void
runInTransaction
(Runnable task) Run the giventask
inside 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 void
Verify 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 aQuery
for the given model class.- Parameters:
klass
- the model class
-
find
Find by primary key.- See Also:
-
findByIds
Find multiple entities by their primary key.
Ensure to check the first-level cache first then the second-level cache and, 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.- Parameters:
klass
- The model classids
- The ids to load- Returns:
- list of all the matched records
- 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 aProperty
of 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 giventask
inside a transaction that is committed after the task is completed.- Parameters:
task
- the task to run.
-
callInTransaction
-
jdbcWork
Perform JDBC related work using theConnection
managed by the currentEntityManager
.- Parameters:
work
- The work to be performed- Throws:
jakarta.persistence.PersistenceException
- Generally indicates wrappedSQLException
-