Package com.jalios.jcms.db
Interface Transaction<T>
- Type Parameters:
T- type of the result object
- All Known Implementing Classes:
LoggableThrowableTransaction,ThrowableTransaction,TranstypingTaskTransaction
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Interface for Hibernate code. To be used with
HibernateUtil.executeTransaction(Transaction), often as anonymous classes within a method
implementation.
Examples:
// Example with id as result
String myDataId = HibernateUtil.executeTransaction(session -> {
MyData myData = new MyData();
myData.setTitle("new title");
[...]
myData.performCreate(admin);
return myData.getId();
});
// Example with domain object as result
// /!\ DO NOT alter or modify the returned object as it belongs to a closed Hibernate session, this approach is not recommended.
MyData myData = HibernateUtil.executeTransaction(session -> {
MyData myData = new MyData();
myData.setTitle("new title");
[...]
myData.performCreate(admin);
return myData;
});
// Example with collection of domain objects as result
// /!\ DO NOT alter or modify the returned objects as they belongs to a closed Hibernate session, this approach is not recommended.
List<MyData> myDataList = HibernateUtil.executeTransaction(session -> {
Criteria criteria = HibernateUtil.getSession().createCriteria(MyData.class);
criteria.add(Restrictions.ilike("title", "%title%"));
return criteria.list();
});
// Example with no result
Article article = ...;
Member member = ... ;
HibernateUtil.executeTransaction(session -> {
VoteManager.getInstance().performVote(article, member, true);
article.trackReader(member);
return null;
});
-
Method Summary
Modifier and TypeMethodDescriptionexecute(org.hibernate.Session session) Gets called byHibernateUtil.executeTransaction(Transaction)with an active Hibernate Session.
-
Method Details
-
execute
Gets called byHibernateUtil.executeTransaction(Transaction)with an active Hibernate Session. Does not need to care about activating or closing the Session, or handling transactions.Allows for returning a result object created within this method, i.e. a domain object or a collection of domain objects. A
TransactionExceptioncan be thrown when there is any Exception: It gets propagated to the caller of theHibernateUtil.executeTransaction(Transaction).- Parameters:
session- session passed by theHibernateUtil.executeTransaction(Transaction)- Returns:
- a result object returned by the action, or null
-