Header Ads

  • Breaking Now

    How to use Annotations with Hibernate?

    Before getting into details of using annotations with Hibernate, I would suggest you to go through the previous example . The example posted here is similar to the old one except it uses annotations.Here .hbm.xml will not be required.

    In addition to existing jar files, following jar files are also required in class path:


    Download these binaries from here

    The annotations are supported Java 5 environment(both by Java SE 5.0 and Java EE 5.0) onwards and having strong coupling with under lying database.In order to support multiple databases, annotations are not meant for that and better to use hibernate mapping files.

    Let us start writing with Department.java class:
    package com.iqjava.department; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="DEPTT") public class Department { private long departmentId; private String departmentName; public Department() { } public Department(String departmentName) { this.departmentName = departmentName; } @Id @GeneratedValue @Column(name="DEPTT_ID") public long getDepartmentId() { return this.departmentId; } public void setDepartmentId(long departmentId) { this.departmentId = departmentId; } @Column(name="DEPTT_NAME", nullable=false) public String getDepartmentName() { return this.departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } }

    Some highlights of the above code:

    - Hibernate supports EJB 3 Annotations persistence which are contained inside javax.persistence package.
    The following annotations are used in the above code:

    -@Entity: represents the Entity bean class.
    -@Table: underlying RDBMS table is mapped with this tag where data is persisted.If this annotation is not mentioned then Hibernate takes class name as default table name
    -@Id: represents identifier property of Entity bean class.It determines default access strategy used by Hibernate.So if @Id is put over field then field access will be used and if it is placed over getter method then property access will be used.
    -@GenertaedValue: specify Primary Key generation strategy
    -@Column: represents the column of the table to which a property is mapped to

    hibernate.cfg.xml looks like as given below:

    <?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"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> <property name="connection.password"></property> <property name="connection.pool_size">1</property> <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="com.iqjava.department.Department" /> </session-factory> </hibernate-configuration>

    In HibernateUtil.java file, AnnotationConfiguration object is used instead of Configuration object.

    package com.iqjava.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }

    Post Top Ad

    Post Bottom Ad