Tuesday, 17 April 2018

Hibernate Example using xml Mapping.............
==========================================================================================
//Employee.java
//Employee.hbm.xml
//hibernet.cfg.xml
//SaveData.java
==============================================================================================
step:--
Create the java project
Add jar files for hibernate
Create the Persistent class
Create the mapping file for Persistent class
Create the Configuration file
Create the class that retrieves or stores the persistent object
Run the application
==========================================================================================
jar file required........
antlr-2.7.5H3.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
postgresql-9.3-1103.jdbc3.jar

============================================================================================
//Employee.java
package govind;

public class Employee {
private int empid;
private String empname;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
============================================================================================
//Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="govind.Employee" table="hibernetemployee" >
<id name="empid" column="empid" >
<generator class="assigned" />
</id>

<property name="empname"  length= "20" column="empname" />
<property name="address"  length= "20" column="empaddress" />
</class>

</hibernate-mapping>

==========================================================================================
//hibernet.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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/java</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">manager</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">30</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
       
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
       
        <property name="hibernate.hbm2ddl.auto">update</property>
       
        <mapping resource="govind/Employee.hbm.xml"/>
       
        <!-- <mapping resource="com/first/hib/emp.hbm.xml"/> -->
    </session-factory>

</hibernate-configuration>

==============================================================================================

package govind;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class SaveData {

public static void main(String[] args) {
Configuration c=new Configuration();
c.configure("/govind/hibernate.cfg.xml");
SessionFactory factory = c.buildSessionFactory();
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
Employee e=new Employee();
e.setEmpid(5);
e.setEmpname("rishabh");
e.setAddress("hanumannagr");
s.persist(e);
tx.commit();
s.close();
System.out.println("Data Saved Successfully");
}

}
===============================================================================================

No comments:

Post a Comment