Hibernate Example using xml mapping........................
================================================================================
//Product .java
//Product.hbm.xml
//hibernate.cfg.xml
//SessionUtility .java
//ProductSaveData .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
================================================================================
//Product .java
package govind;
public class Product {
private int productid;
private String productname;
private float productprice;
public int getProductid() {
return productid;
}
public void setProductid(int productid) {
this.productid = productid;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public float getProductprice() {
return productprice;
}
public void setProductprice(float productprice) {
this.productprice = productprice;
}
}
===============================================================================
//Product.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.Product" table="producthibernate" >
<id name="productid" column="productid" >
<generator class="assigned" />
</id>
<property name="productname" length= "20" column="productname" />
<property name="productprice" length= "20" column="productprice" />
</class>
</hibernate-mapping>
================================================================================
//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>
<!-- 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/Product.hbm.xml"/>
<!-- <mapping resource="com/first/hib/emp.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
================================================================================
//SessionUtility .java
package govind;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtility {
static SessionFactory sf=null;
static ThreadLocal<Session> session=new ThreadLocal<Session>();
static
{
try
{
Configuration c=new Configuration();
c.configure("/govind/hibernate.cfg.xml");
sf = c.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Session getSession()
{
Session s = session.get();
if(s==null)
{
s=sf.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = session.get();
if(s!=null)
{
session.set(null);
}
}
}
=================================================================================
//ProductSaveData .java
package govind;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class ProductSaveData {
public static void main(String[] args) {
try
{
Product p=new Product();
p.setProductid(6);
p.setProductname("book");
p.setProductprice(1000.00f);
Session session = SessionUtility.getSession();
Transaction tx = session.beginTransaction();
//session.save(p);
//session.persist(p);
session.saveOrUpdate(p);
tx.commit();
System.out.println("Insert Record Successfully");
SessionUtility.closeSession();
}
catch (Exception e) {
System.out.println("Record Doesn't Insert Successfully");
e.printStackTrace();
}
}
}
===============================================================================
================================================================================
//Product .java
//Product.hbm.xml
//hibernate.cfg.xml
//SessionUtility .java
//ProductSaveData .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
================================================================================
//Product .java
package govind;
public class Product {
private int productid;
private String productname;
private float productprice;
public int getProductid() {
return productid;
}
public void setProductid(int productid) {
this.productid = productid;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public float getProductprice() {
return productprice;
}
public void setProductprice(float productprice) {
this.productprice = productprice;
}
}
===============================================================================
//Product.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.Product" table="producthibernate" >
<id name="productid" column="productid" >
<generator class="assigned" />
</id>
<property name="productname" length= "20" column="productname" />
<property name="productprice" length= "20" column="productprice" />
</class>
</hibernate-mapping>
================================================================================
//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>
<!-- 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/Product.hbm.xml"/>
<!-- <mapping resource="com/first/hib/emp.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
================================================================================
//SessionUtility .java
package govind;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtility {
static SessionFactory sf=null;
static ThreadLocal<Session> session=new ThreadLocal<Session>();
static
{
try
{
Configuration c=new Configuration();
c.configure("/govind/hibernate.cfg.xml");
sf = c.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Session getSession()
{
Session s = session.get();
if(s==null)
{
s=sf.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = session.get();
if(s!=null)
{
session.set(null);
}
}
}
=================================================================================
//ProductSaveData .java
package govind;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class ProductSaveData {
public static void main(String[] args) {
try
{
Product p=new Product();
p.setProductid(6);
p.setProductname("book");
p.setProductprice(1000.00f);
Session session = SessionUtility.getSession();
Transaction tx = session.beginTransaction();
//session.save(p);
//session.persist(p);
session.saveOrUpdate(p);
tx.commit();
System.out.println("Insert Record Successfully");
SessionUtility.closeSession();
}
catch (Exception e) {
System.out.println("Record Doesn't Insert Successfully");
e.printStackTrace();
}
}
}
===============================================================================
No comments:
Post a Comment