Hibernate Annotation Tutorial

As in my previous post, right now i’m working on a project that use Hibernate annotation. I feel great and really believe that i am making a right choice. So, i want to share my experience using it here.

Suppose you have one simple Table called Unit.
Unit has 4 field ID,NAME,CREATE_BY and LOCATION.
First, is create a hibernate configuration file hibernate.cfg.xml

<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”&gt;
<hibernate-configuration>
<session-factory>
<!– Database connection settings –>
<property name=”connection.driver_class”>[driver]</property>
<property name=”connection.url”>[url]</property>
<property name=”connection.username”>[username]</property>
<property name=”connection.password”>[password]</property>
<!– JDBC connection pool (use the built-in) –>
<property name=”connection.pool_size”>1</property>
<!– SQL dialect –>
<property name=”dialect”>org.hibernate.dialect.OracleDialect</property>
<!– Enable Hibernate’s automatic session context management –>
<property name=”current_session_context_class”>thread</property>
<!– Disable the second-level cache –>
<property name=”cache.provider_class”>org.hibernate.cache.NoCacheProvider</property>
<!– Echo all executed SQL to stdout –>
<property name=”show_sql”>true</property>
<!– Drop and re-create the database schema on startup –>
<property name=”hbm2ddl.auto”>none</property>
<mapping class=”com.vico.vems.beans.Unit”/>
</session-factory>
</hibernate-configuration>

Next,you create pojo class to map the table.
*updated 24-June-2011 thanks to Giri

@Entity
@Table(name=”UNITS_PER_ASSET”)

public class Unit {
private Integer id;
private String name;
private String createBy;
private String location;

public Unit(){}

@Column(name=”CREATE_BY”)
public String getCreateBy() {
return createBy;
}

public void setCreateBy(String createBy) {
this.createBy = createBy;
}

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = “SEQ_UNIT”)
@SequenceGenerator(name=”SEQ_UNIT”, sequenceName = “SEQ_UNIT”)
@Column(name=”ID”)
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name=”LOCATION_ID”)
public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

@Column(name=”NAME”)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Remember, all annotation is from javax.persistence.* and not from org.hibernate.*
Little explanation,
@Entity is always used if you want the class to be recognize by hibernate
@Table is used when your class name isn’t the same with your table name
@Id is always used because hibernate think this is a mandatory
@Column is used if your field variable differ than your field column

Here i use @GeneratedValue because i had a sequence in my table to fill my ID column.

Next is to create simple class to test save and load method.
public class HibernateTest {
private static final Logger logger = Logger.getLogger(HibernateTest.class);
public static void main(String[] args){
AnnotationConfiguration ac = new AnnotationConfiguration();
Session session = ac.configure().buildSessionFactory().openSession();

List<Unit> list = new ArrayList<Unit>();

Transaction tx = session.beginTransaction();
for(int i=0;i<10;i++){
Unit u = new Unit();
u.setId((i+50000));
u.setName(“@”+i);
u.setLocation(“Badak”);
u.setCreateBy(“12345”);
session.save(u);
}
session.flush();
tx.commit();

list = session.createQuery(“from Unit u where u.location=:loc and u.name like ‘@%'”)
.setString(“loc”, “Badak”)
.list();

logger.debug(“list size = “+list.size());
for (Unit object : list) {
logger.debug(“Unit = “+object.getName());
}
}
}