Results 1 to 2 of 2
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default Hibernate boilerplate

    Hey,


    is there a way to eliminate/simplify hibernate bolierplate code?

    I have a class EventManager and all the functions in there consist of basically the same code

    example:
    Java Code:
    function:1
    public final List  byName(String name){
            Transaction transaction = null;
            Session session =   HibernateUtil.getSessionFactory().getCurrentSession();
            try{
                transaction = session.beginTransaction();
                 //some code here
                transaction.commit();
            }catch(RuntimeException e){
                if(transaction != null && transaction.isActive()){
                    try{
                        transaction.rollback();
                    }catch(HibernateException e1){
                        e1.printStackTrace();
                    }
                }
            }
            
        }
    
    function:2
    public final void save(){
            Transaction transaction = null;
            Session session =   HibernateUtil.getSessionFactory().getCurrentSession();
            try{
                transaction = session.beginTransaction();
                 //some code here
                transaction.commit();
            }catch(RuntimeException e){
                if(transaction != null && transaction.isActive()){
                    try{
                        transaction.rollback();
                    }catch(HibernateException e1){
                        e1.printStackTrace();
                    }
                }
            }
            
        }
    function: 3
    public final List<Employee> byDepartment(String department){
            Transaction transaction = null;
            Session session =   HibernateUtil.getSessionFactory().getCurrentSession();
            try{
                transaction = session.beginTransaction();
                 //some code here
                transaction.commit();
            }catch(RuntimeException e){
                if(transaction != null && transaction.isActive()){
                    try{
                        transaction.rollback();
                    }catch(HibernateException e1){
                        e1.printStackTrace();
                    }
                }
            }
            
        }
    function: 4.
    ....
    and so on

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    There's not much you can do with the session and transaction stuff, but you could stick the standardised rollbackTransaction() code as its own method.

    I do hope you are not actually eating that RuntimeException...:)

Similar Threads

  1. Hibernate, help me
    By Nick15 in forum JDBC
    Replies: 2
    Last Post: 06-05-2008, 03:39 PM
  2. Hibernate Use
    By sireesha in forum JDBC
    Replies: 2
    Last Post: 05-21-2008, 03:05 AM
  3. hibernate
    By vipinpvijayan in forum Web Frameworks
    Replies: 1
    Last Post: 04-21-2008, 05:41 PM
  4. Replies: 2
    Last Post: 06-30-2007, 03:01 PM
  5. Hibernate
    By Shuru in forum JDBC
    Replies: 2
    Last Post: 05-09-2007, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •