For inserting record into database we have to follow the following steps

Step 1: Create table from your database(i am using MYSQL )

for doing any operations in hibernate we have to  create table with primary key

create table employee(id int(10)  primary key(id),name varchar(30),address varchar(30));

now table is created as below



















Step 2: create POJO class with table fields

package a;

public class Employee {

    int id;
    String name;
    String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}


Step 3: generate hibernate configuration file (hibernate.cfg.xml)




<?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/xxx</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="a/Employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>




package a.b;

import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Insert {
public static void main(String ar[])    {
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session hs=sf.openSession();
org.hibernate.Transaction tx=hs.beginTransaction();

Employee e=new Employee();
e.setId(2);
e.setName("ffjkvgynfjbkr");
e.setAddress("dubai");

hs.save(e);
tx.commit();
System.out.println("record is inserted jgsdsjdgsajd");
hs.close();
sf.close();
}
}


Step 7: generate hibernate mapping file from database table 



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 8, 2013 10:36:24 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="a.Employee" table="employee" catalog="xxx">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" />
        </property>
        <property name="address" type="string">
            <column name="address" length="30" />
        </property>
    </class>
</hibernate-mapping>


now run project






then check your database





0 comments:

Post a Comment