Package com.axelor.db
Class JPA
- java.lang.Object
-
- com.axelor.db.JPA
-
@Singleton public final class JPA extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
JPA.JDBCWork
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Model>
Query<T>all(Class<T> klass)
Prepare aQuery
for the given model class.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(Class<T> klass, Map<String,Object> values)
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.static javax.persistence.EntityManager
em()
Get an instance ofEntityManager
.static int
execute(String query)
Execute a JPQL update query.static <T extends Model>
Propertyfield(Class<T> klass, String name)
Return aProperty
of the given model class.static <T extends Model>
Property[]fields(Class<T> klass)
Return all the properties of the given model class.static <T extends Model>
Tfind(Class<T> klass, Long id)
Find by primary key.static <T extends Model>
List<T>findByIds(Class<T> klass, List<Long> ids)
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<?>
model(String name)
Return the model class for the given name.static Set<Class<?>>
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(Class<? extends Model> model, Map<String,Object> values)
Verify the values against the database values to ensure the records involved are not modified.static <T> T
withTransaction(Supplier<T> task)
Run the giventask
inside a transaction that is committed after the task is completed and return the task result.
-
-
-
Method Detail
-
em
public static javax.persistence.EntityManager em()
Get an instance ofEntityManager
.
-
execute
public static int execute(String query)
Execute a JPQL update query.- Parameters:
query
- JPQL query
-
all
public static <T extends Model> Query<T> all(Class<T> klass)
Prepare aQuery
for the given model class.- Parameters:
klass
- the model class
-
find
public static <T extends Model> T find(Class<T> klass, Long id)
Find by primary key.- See Also:
EntityManager.find(Class, Object)
-
findByIds
public static <T extends Model> List<T> findByIds(Class<T> klass, List<Long> ids)
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:
MultiIdentifierLoadAccess
-
persist
public static <T extends Model> T persist(T entity)
Make an entity managed and persistent.- See Also:
EntityManager.persist(Object)
-
merge
public static <T extends Model> T merge(T entity)
Merge the state of the given entity into the current persistence context.- See Also:
EntityManager.merge(Object)
-
save
public static <T extends Model> T save(T entity)
Save the state of the entity.
It uses eitherpersist(Model)
ormerge(Model)
and callsflush()
to synchronize values with database.- See Also:
persist(Model)
,merge(Model)
-
remove
public static <T extends Model> void remove(T entity)
Remove the entity instance.- See Also:
EntityManager.remove(Object)
-
refresh
public static <T extends Model> void refresh(T entity)
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.- See Also:
EntityManager.refresh(Object)
-
flush
public static void flush()
Synchronize the persistence context to the underlying database.- See Also:
EntityManager.flush()
-
clear
public static void clear()
Clear the persistence context, causing all managed entities to become detached.- See Also:
EntityManager.clear()
-
verify
public static void verify(Class<? extends Model> model, Map<String,Object> values)
Verify the values against the database values to ensure the records involved are not modified.- Throws:
javax.persistence.OptimisticLockException
- if version mismatch of any a record is deleted
-
edit
public static <T extends Model> T edit(Class<T> klass, Map<String,Object> values)
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
public static <T extends Model> T manage(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.- Parameters:
bean
- model instance- Returns:
- JPA managed model instance
- See Also:
edit(Class, Map)
-
models
public static Set<Class<?>> models()
Return all the non-abstract models found in all the activated modules.- Returns:
- Set of model classes
-
model
public static Class<?> model(String name)
Return the model class for the given name.- Parameters:
name
- name of the model- Returns:
- model class
-
fields
public static <T extends Model> Property[] fields(Class<T> klass)
Return all the properties of the given model class.
-
field
public static <T extends Model> Property field(Class<T> klass, String name)
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
public static <T extends Model> T copy(T bean, boolean deep)
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
public static void runInTransaction(Runnable task)
Run the giventask
inside a transaction that is committed after the task is completed.- Parameters:
task
- the task to run.
-
withTransaction
public static <T> T withTransaction(Supplier<T> task)
Run the giventask
inside a transaction that is committed after the task is completed and return the task result.- Type Parameters:
T
-- Parameters:
task
-- Returns:
- task result
-
jdbcWork
public static void jdbcWork(JPA.JDBCWork work)
Perform JDBC related work using theConnection
managed by the currentEntityManager
.- Parameters:
work
- The work to be performed- Throws:
javax.persistence.PersistenceException
- Generally indicates wrappedSQLException
-
-