Interface Transaction<T>

  • Type Parameters:
    T - type of the result object

    public interface Transaction<T>
    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(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;
       }
     );