Java Hibernate 

Rules available in this category:

  1. Always_call_addClass_instead_of_addResource_to_add_a_mapping_to_a_Configuration
  2. Always_roll_back_any_active_transactions_in_catch_blocks

Rule 1: Always_call_addClass_instead_of_addResource_to_add_a_mapping_to_a_Configuration

Severity:  Medium
Rule:  Using the addClass method can be better than using the addResource method since it eliminates the need for hardcoded mapping filenames.
Reason:  Using the addClass method can be better than using the addResource method since it eliminates the need for hardcoded mapping filenames.

Usage Example: 

public class Test
{
    public void initConfig() 
   {
        Configuration cfg = new Configuration().addResource("myFile.hbm.xml");  //VIOLATION
    }
}

Should be written as:

public class Test
{
    public void initConfig() 
   {
        Configuration cfg = new Configuration().addClass(MyFile.class);  //FIXED
    }
}

class MyFile
{
    //some implementation of a mapped MyFile class
}

Reference:  http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html

Rule 2: Always_roll_back_any_active_transactions_in_catch_blocks

Severity:  Medium
Rule:  If a Hibernate transaction is started in a try block, one should make sure to roll back that transaction in case of an exception.
Reason:  If a Hibernate transaction is started in a try block, one should make sure to roll back that transaction in case of an exception.

Usage Example: 

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
         

public class Test 
{
    public static void main(String[] args) 
   {
        Session session = null;
        SessionFactory sessionFactory = new Configuration().configure()              .buildSessionFactory();
        Transaction tx = null;
         

        try 
  {
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
//...
            tx.commit();
        } 
  catch (HibernateException e)          //VIOLATION
  {
            System.out.println(e.getMessage());
         } 
   finally 
   {
            session.flush();
            session.close();
        }
    }
}

Should be written as:

public class Test 
{
    public static void main(String[] args) 
    {
        Session session = null;
        SessionFactory sessionFactory = new Configuration().configure()
                .buildSessionFactory();
        Transaction tx = null;
         
        try 
  {
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
//...         
            tx.commit();
         } 
   catch (HibernateException e) 
   {        
            if (tx!= null) 
{
                tx.rollback();             // FIXED
            }
            System.out.println(e.getMessage());
          } 
    finally 
    {
            session.flush();
            session.close();
        }
    }
}

Reference:  http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html