Sunday, July 27, 2014

Hibernate For Entity Beans of EJB

Hibernate also support Entity beans and Java Persistence Provider(JPA) of EJB3.0+ specification. In fact many of the concepts of Entity beans has been taken from Hibernate. JBoss application server uses Hibernate as the default persistence provider for Entity beans and JPA. JPA has the notion of persistence unit which represents the configuration.

EntityManagerFactory of JPA corresponds to SessionFactory and EntityManager corresponds to Session. Let's do the ((Hibernate Introduction with Annotation)) example of persisting Student object using Entity beans and JPA API's

Now let's write our domain class which is Student class.Note that it the same class as we did in Hibernate Introduction with Annotation


//Entity annotation represents that it is an entity class and is
//mapped to a table in database. Table annotation tells about the 
//table name to which this entity is mapped
public class Student {
    //Id will correspond the primary key in the database
    private Long id;
    protected String name;
    //Id - Represents that it is a primary key column
    //GeneratedValue - How the key to be generated
    //column - Column to which this property is mapped
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    public void setId(Long id) { = id;
    //There is annotation here so by default it is mapped to
    //the column with name NAME. In annotation, the properties are 
    //by default mapped. In XML mapping by default the columns
    //are not mapped.
    public String getName() {
        return name;
    public void setName(String name) { = name;

Now let's write persistence.xml. This is the configuration file that EJB mandates. It is similar to what hibernate configuration file does. It can support multiple persistence provider other than hibernate also.

persistence.xml (Put in META-INF folder under source folder)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   <!-- This name is used to refer to this persistence context.
    This will be used while we make the EntityManagerFactory -->
    <persistence-unit name="hibernateMapping">
        <!-- Persistence provider --> 
       <!-- Provide properites of persistence provider. This is
         specific to each persistence provider. All hibernate related properties 
        which sit in hibernate.cfg.xml can be put here. -->
           <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
           <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>

Now we just have to write our main class and start interacting with database.

public class HibernateMappingJPA {
    public static void main(String[] args) {

        //The name hibernateMapping comes from persistence.xml
        EntityManagerFactory emf =
        //EntityManager is similar to Session in hibernate
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        Student student = new Student();
        student.setName("James Bond");
        //Query API
        Query query = em.createQuery("Select s from Student s where like ?");
        query.setParameter(1, "James%");
        List<Student> studentList = query.getResultList();
        for(Student s :studentList){

Note that we do not have to map the entity class explicitly as these are scanned automatically based on the @Entity annotation.

More write-ups on Hibernate

No comments:

Post a Comment