Tutorials
Spring Struts 2 Hibernate Hibernate JPA jQuery Struts 1 Java Server Faces (JSF) JDBC Servlet JSP Java Ajax Dojo JavaScript HTML XML PHP Core PHP Advance MySQL Interview Questions
Advertise With Us
Previous
Website design tutorial Golf Swing Create a website
Home
Next
In this section, you will learn to insert data into database through hibernate. Follows the following steps:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration3.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 /hibernateExamples</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Step 2:Create Simple bean class like setter or getter. Employee.java
package developerhelpway.hibernate; public class Employee { private int empId; private String empName;
private int empSal; 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 int getEmpSal() { return empSal; } public void setEmpSal(int empSal) { this.empSal = empSal; } }
Step 3: Then you need mapping file cross-ponding to Employee.java. The mapping file will use when you work with database at the time of transaction. employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernatemapping-3.0.dtd"> <hibernate-mapping> <class name="developerhelpway.hibernate.Employee" table="employee"> <id name="empId" type="int" column="emp_id" > <generator class="increment"/> </id> <property name="empName"> <column name="emp_name" /> </property>
package developerhelpway.hibernate; import import import import org.hibernate.Session; org.hibernate.SessionFactory; org.hibernate.Transaction; org.hibernate.cfg.Configuration;
public class CreateHibernateExample { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; Transaction tran = null; try{ SessionFactory sessFact = new Configuration() .configure().buildSessionFactory(); sess = sessFact.openSession(); tran = sess.beginTransaction(); Employee emp = new Employee(); emp.setEmpName("Birendra Kumar"); emp.setEmpSal(12000); sess.save(emp); tran.commit(); } catch(Exception ex){ ex.printStackTrace(); } finally{ sess.close(); } } }
Output:
Previous
Good Programming Tips Advertise Here
Advertise with Us Add Place
Home
Next
Click here