hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <?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.dialect"<org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ebhor</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping resource="Pojo/Employee.hbm.xml"/> </session-factory> </hibernate-configuration> |
Employee.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Pojo.Employee" table="employee" catalog="ebhor"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="firstName" type="string"> <column name="FirstName" length="45" not-null="true" /> </property> <property name="lastName" type="string"> <column name="LastName" length="45" not-null="true" /> </property> <property name="email" type="string"> <column name="Email" length="45" not-null="true" /> </property> </class> </hibernate-mapping> |
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package Pojo; public class Employee implements java.io.Serializable { private Integer id; private String firstName; private String lastName; private String email; public Employee() { } public Employee(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } } |
EmpInsert.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package HibClient; import Pojo.Employee; import org.hibernate.cfg.Configuration; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.Session; import org.hibernate.HibernateException; public class EmpInsert { public static void main(String args[]) { try { // This step will read hibernate.cfg.xml and prepare hibernate for use Configuration conf = new Configuration(); Configuration cf = conf.configure(); SessionFactory factory = cf.buildSessionFactory(); Session session = factory.openSession(); System.out.println("++++++++++++++"); //Create new instance of Contact and set values in it Employee emp = new Employee(); emp.setFirstName("anurag"); emp.setLastName("Singh"); System.out.println("insertion"); Transaction tx=session.beginTransaction(); session.save(emp); tx.commit(); session.close(); } catch (HibernateException e) { System.out.println(" "+e.getMessage()); e.printStackTrace(); } } } |
Whenever this program get compiled and run this application as simple java application run same this client application insert the records in database.