Hibernate 4 annotation Example

1.Employee.java file:

package com.vy.Employees;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Employees")
public class Employee {
 
 @Id
 @GeneratedValue
 @Column(name="ID")
 private int empId;
 
 @Column(name="NAME")
 private String empName;
 
 @Column(name="SALARY")
 private double empSalary;

 public int getEmpId() {
  return empId;
 }

 public void setEmpId(int empId) {
  this.empId = empId;
 }

 public String getEmpName() {
  return empName;
 }

 public void setEmpName(String empName) {
  this.empName = empName;
 }

 public double getEmpSalary() {
  return empSalary;
 }

 public void setEmpSalary(double empSalary) {
  this.empSalary = empSalary;
 }
 
}

2.AddEmployee.java file

package com.vy.Employees;
import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;

public class AddEmployee {
 public static void main(String[] args) {
 Configuration configuration= new Configuration();
 configuration.configure("hibernate.cfg.xml");
 configuration.addAnnotatedClass(Employee.class);
 StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder();
 ssrb.applySettings(configuration.getProperties());
 StandardServiceRegistry ssr= ssrb.build();
 SessionFactory sf= configuration.buildSessionFactory(ssr);
 Employee e= new Employee();
 e.setEmpName("sudheer kumar");
 e.setEmpSalary(22000);
 Session session= sf.openSession();
 Transaction tx= session.beginTransaction();
 session.save(e);
 System.out.println("object saved successfully");
 tx.commit();
 session.close();
 sf.close();
 }
}

3.hibernate.cfg.xml file:



  
    
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost/hb
    root
    

    
    org.hibernate.dialect.MySQLDialect

    
    true

    
    update
    
  



the following are the required jar files to work with Hibernate:
hibernate-release-4.3.8.Final\lib\required\antlr-2.7.7.jar
hibernate-release-4.3.8.Final\lib\required\dom4j-1.6.1.jar
hibernate-release-4.3.8.Final\lib\required\hibernate-commons-annotations-4.0.5.Final.jar
hibernate-release-4.3.8.Final\lib\required\hibernate-core-4.3.8.Final.jar
hibernate-release-4.3.8.Final\lib\required\hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-release-4.3.8.Final\lib\required\jandex-1.1.0.Final.jar
hibernate-release-4.3.8.Final\lib\required\javassist-3.18.1-GA.jar
hibernate-release-4.3.8.Final\lib\required\jboss-logging-3.1.3.GA.jar
hibernate-release-4.3.8.Final\lib\required\jboss-logging-annotations-1.2.0.Beta1.jar
hibernate-release-4.3.8.Final\lib\required\jboss-transaction-api_1.2_spec-1.0.0.Final.jar
mysql-connector-java-5.0.8\mysql-connector-java-5.0.8-bin.jar

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.