Package com.jalios.jcms.db
Interface Transaction<T>
-
- Type Parameters:
T
- type of the result object
public interface Transaction<T>
Interface for Hibernate code. To be used withHibernateUtil.executeTransaction(Transaction)
, often as anonymous classes within a method implementation.Examples:
// Example with id as result String myDataId = HibernateUtil.executeTransaction(new Transaction<String>() { @Override public String execute(Session session) { MyData myData = new MyData(); myData.setTitle("new title"); [...] myData.performCreate(admin); return myData.getId(); } }); // Example with domain object as result MyData myData = HibernateUtil.executeTransaction(new Transaction<MyData>() { @Override public MyData execute(Session session) { MyData myData = new MyData(); myData.setTitle("new title"); [...] myData.performCreate(admin); return myData; } }); // Example with collection of domain objects as result List<MyData> myDataList = HibernateUtil.executeTransaction(new Transaction<List<MyData>>() { @Override public List<MyData> execute(Session session) { Criteria criteria = HibernateUtil.getSession().createCriteria(MyData.class); criteria.add(Restrictions.ilike("title", "%title%")); return criteria.list(); } }); // Example with no result HibernateUtil.executeTransaction(new Transaction<Void>() { } {@literal @}Override public Void execute(Session session) { Member member = voterMembers.get(index); VoteManager.getInstance().performVote(article, member, true); article.trackReader(member); return null; } );
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description T
execute(org.hibernate.Session session)
Gets called byHibernateUtil.executeTransaction(Transaction)
with an active Hibernate Session.
-
-
-
Method Detail
-
execute
T execute(org.hibernate.Session session)
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
TransactionException
can 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
-
-