Hibernate Example With Annotation
This example is the same as the first example except that it uses annotations.In our first example we created .hbm.xml file for database and pojo class mapping, here there is no need to create hbm files, instead we will use annotations to do the object relational mapping.
Steps to create the hibernate application with Annotation
- Add the jar file
- Create the Persistent class
- Add mapping of Persistent class in configuration file
- Create the class that retrieves or stores the persistent object
This example is the same as the first example except that it uses annotations.In our first example we created .hbm.xml file for database and pojo class mapping, here there is no need to create hbm files, instead we will use annotations to do the object relational mapping.
Steps to create the hibernate application with Annotation
- Add the jar file
- Create the Persistent class
- Add mapping of Persistent class in configuration file
- Create the class that retrieves or stores the persistent object
Step 1: Add the jar file for annotation
In addition to the already existing jar files you need to add the following jar files to the classpath.
1.hibernate-commons-annotations.jar 2.ejb3-persistence.jar 3.hibernate-annotations.jar
Following jar files are enough for getting annotation support in hibernate.
1.antlr-2.7.6.jar 2.commons-collections-3.1.jar 3.dom4j-1.6.1.jar 4.hibernate-commons-annotations-3.2.0.Final.jar 5.hibernate-core-3.6.7.Final.jar 6.hibernate-jpa-2.0-api-1.0.1.Final.jar 7.javassist.jar 8.jms-1.1.jar 9.jsr250-api-1.0.jar 10.jta-1.1.jar 11.log4j-1.2.16.jar 12.mysql connector.jar 13.slf4j-api-1.6.1.jar 14.slf4j-log4j12-1.6.1.jar
Step 2 : Create the Persistent class
Student.java
package com.jwt.hibernate; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "STUDENT") public class Student { private long id; private String name; private String degree; private String roll; private String phone; @Id public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public String getRoll() { return roll; } public void setRoll(String roll) { this.roll = roll; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
In the above class, we are creating the same persistent class which we have created in the previous example. But here, we are using annotation.
@Entity annotation marks this class as an entity.
@Table annotation specifies the table name where data of this entity is to be persisted. If you are not using @Table annotation in Entity class, hibernate will use the class name as the table name by default.
@Id annotation marks the identifier for this entity.
@Column annotation specifies the details of the column for this property or field. If @Column annotation is not specified, property name will be used as the column name bydefault.
Step 3 : Mapping of Persistent class in configuration file
The next change you need to do here is, instead of adding the .hbm.xml file to the hibernate.cfg.xml file, we add the fully qualified name of the annotated class to the mapping element.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javawebtutor</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mukesh</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create </property> <mapping class="com.jwt.hibernate.Student" /> </session-factory> </hibernate-configuration>
Step 4 : Create Test class that retrieves or stores the persistent object
Test.java
package com.jwt.hibernate.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import com.jwt.hibernate.Student; public class Test { public static void main(String[] args) { Session session = new AnnotationConfiguration().configure() .buildSessionFactory().openSession(); Transaction t = session.beginTransaction(); Student student = new Student(); student.setId(1); student.setName("Mukesh"); student.setRoll("101"); student.setDegree("B.E"); student.setPhone("99999"); Student student1 = new Student(); student.setId(2); student.setName("Ravi"); student.setRoll("102"); student.setDegree("B.E"); student.setPhone("934499"); session.persist(student); session.persist(student1); t.commit(); session.close(); System.out.println("successfully saved"); } }
Output :
If data is inserted into DB you can see generated sql in console as given below