Java Hibernate - Part 3- Annotations Example

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

Requirements:

 Eclipse Keplor, MySQL 5.5,mysql-connector-odbc-5.2.5, Hibernate 3.2 (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. HibernateUtil.java
  6. write the following client side code under package com.sam.hibernatefiles.
    1. Lab1A

Customer.java

package com.sam.hibernatefiles;
import javax.persistence.*;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="cid")
private int cid;
@Column(name="cname")
private String cname;
@Column(name="email")
private String email;
@Column(name="phone")
private long phone;
@Column(name="city")
private String city;
@Column(name="bal")
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 class="com.sam.hibernatefiles.Customer"/>
</session-factory>
</hibernate-configuration>

HibernateUtil.java


package com.sam.hibernatefiles;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory factory;
static
{
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg=(AnnotationConfiguration)cfg.configure();
factory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}

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 displayed");
}
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