Interface Transaction<T>

  • Type Parameters:
    T - type of the result object
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    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(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;
     });