package com.hibernate.example;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Employee {
private int empid;
private String empname;
@Id
@GeneratedValue
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;
}
}
package com.hibernate.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEmployee {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);
new SchemaExport(config).create(true,true);
Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
session.close();
}
}
No comments:
Post a Comment