Java Hibernate- Part 2- Simple Example


Program to add Customer details in to MySQL Database using Hibernate 3.1.

Requirements:

 Eclipse Helios, MySQL 5.5,mysql-connector-odbc-5.2.5, Hibernate 3.1 (jars and xml files).

Steps


  1. create a new java project named Sample1
  2. add all hibernate jars to project build path.
  3. copy hibernate.cfg.xml to src folder
  4. Open MySQL command prompt 
    1. create Test database and use it.
    2. create Table named customer.
      • create table customer(cid int primary key auto_increment,cname varchar(20),email varchar(20),phone varchar(15),city varchar(15),bal double);
  5. create a package named com.sam.hibernatefiles and write the following
    1. Customer.java(Hibernate Persistence class)
    2. Customer.hbm.xml (Hibernate mapping document to map your persistence class with table)
    3. HibernateUtil.java
  6. write the following client side code under package com.sam.hibernatefiles.
    1. Lab1A

Customer.java

package com.sam.hibernatefiles;

public class Customer {
private int cid;
private String cname;
private String email;
private long phone;
private String city;
private double bal;
public Customer() {
}
public Customer(String cname, String email, long phone,
String city, double bal) {
super();
this.cid = cid;
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
this.bal = bal;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public double getBal() {
return bal;
}
public void setBal(double bal) {
this.bal = bal;
}
}

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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">minu</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/sam/hibernatefiles/Customer.hbm.xml"/>
</session-factory> 
    
    
</hibernate-configuration>


HibernateUtil.java

package com.sam.hibernatefiles;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory factory;
static
{
try
{
Configuration cfg=new Configuration();
cfg=cfg.configure();
factory=cfg.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}

Customer.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 package="com.sam.hibernatefiles">

<class name="Customer" table="customer">
<id name="cid" column="cid" type="int">
<generator class="increment"/>
</id>
<property name="cname" column="cname" type="string"/>
<property name="email" column="email" type="string"/>
<property name="phone" column="phone" type="long"/>
<property name="city" column="city" type="string"/>
<property name="bal" column="bal" type="double"/>
</class>

</hibernate-mapping>

Lab1A.java

package com.sam.hibernatefiles;

import org.hibernate.*;

public class Lab1A {
public static void main(String[] args) {
Transaction tx=null;
try
{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session sess=sf.openSession();
tx=sess.beginTransaction();
Customer c=new Customer("minu","minu999@gmail.com",9876543210L,"blore",11000.0);
Customer c1=new Customer("anu","anu999@gmail.com",9876743210L,"blore",12000.0);
Customer c2=new Customer("alen","alen99@gmail.com",9876543510L,"dufai",19000.0);
sess.save(c);
sess.save(c1);
sess.save(c2);
tx.commit();
sess.close();
System.out.println("REcord inserted");
}
catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}
}
}






Lab1B.java (for loading Table contents row wise to client)



package com.sam.hibernatefiles;

import org.hibernate.*;


public class Lab1B {
public static void main(String[] args) {
Transaction tx=null;
try
{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session sess=sf.openSession();
tx=sess.beginTransaction();
Customer cc=(Customer)sess.load(Customer.class, 1); //1st row
System.out.println(cc.getCid()+"\t"+cc.getCname()+"\t"+cc.getEmail()+"\t"+cc.getPhone()+"\t"+cc.getCity()+"\t"+cc.getBal());
tx.commit();
sess.close();
System.out.println("REcord inserted");
}
catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}
}
}




Comments

Popular posts from this blog

Hibernate Collection Mapping example

C programming:-Files - Part 5

Struts 2 Troubleshooting