Sunday, January 23, 2011

Hibernate - many to one example

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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
  <property name="hibernate.connection.username">test</property>
  <property name="hibernate.connection.password">test</property>
       
  <!-- SQL dialect -->
  <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>

  <mapping resource="vendor.hbm.xml"/>     
  <mapping resource="customer.hbm.xml"/>       

</session-factory>
</hibernate-configuration>

customer.hbm.xml  :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Customer" table="CUST">
<id name="cid" column="CID">
</id>
<property name="cnm" column="CNM" type="string"/>
<property name="vidfk" insert="false" update="false"/>
<many-to-one name="parent" class="Vendar" cascade="all" column="VIDFK"></many-to-one>
</class> 

</hibernate-mapping>

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

<hibernate-mapping>

<class name="Vendar" table="VEND">
    <id name="vid" column="VID">
    <generator class="increment" />   
    </id>
    <property name="vnm" column="VNAME" type="java.lang.String"/>
</class>

</hibernate-mapping>
Customer.java :
package com.test.hibernate;

/**
 * @author Mallik
 *
 */
public class Customer {
   
    /**
     * DataMembers Declaration
     *
     */
    private int cid;
    private String cnm;
    private int vidfk;
    private Vendar parent;
   
    //setters&getters
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getCnm() {
        return cnm;
    }
    public void setCnm(String cnm) {
        this.cnm = cnm;
    }
    public int getVidfk() {
        return vidfk;
    }
    public void setVidfk(int vidfk) {
        this.vidfk = vidfk;
    }
    public Vendar getParent() {
        return parent;
    }
    public void setParent(Vendar parent) {
        this.parent = parent;
    }
}
Vendar.java :
package com.test.hibernate;

/**
 * @author Mallik
 *
 */
import java.util.*;
public class Vendar {
    /**
     * DataMembers Declaration
     *
     */
private int vid;
private String vnm;

//setters&getters
public int getVid() {
    return vid;
}
public void setVid(int vid) {
    this.vid = vid;
}
public String getVnm() {
    return vnm;
}
public void setVnm(String vnm) {
    this.vnm = vnm;
}   
}

Save.java :
/**
 * @author Mallik
 *
 */
package com.test.hibernate;

import java.util.*;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class Save {

    /**
     * @param args
     */
   
  public static void main(String[] args) {
       
  Session ses=new Configuration().configure().buildSessionFactory().openSession();
      Vendar v=new Vendar();
       v.setVid(11);
       v.setVnm("kiran");
       
        Customer c1=new Customer();
        Customer c2=new Customer();
        c1.setCid(177);
        c1.setCnm("ram");
        c1.setParent(v);
        c1.setVidfk(v.getVid());
       
        c2.setCid(175);
        c2.setCnm("ram1");
        c2.setParent(v);
        c2.setVidfk(v.getVid());   
       
        Transaction transaction = null;
   
        try {
        transaction = ses.beginTransaction();
        ses.save(c1);
        ses.save(c2);
        transaction.commit();
        System.out.println("successfull !!!!!!!!!!!!!!!!!!!!!!!!!!! ");
        } catch (Exception e)
        {
            System.out.println(e);
        if (transaction != null) {
        transaction.rollback();       
        }
        } finally {
        ses.close();
        }
    }
}

Select.java :
package com.test.hibernate;

/**
 * @author Mallik
 *
 */
import java.util.*;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class Select {   
    public static void main(String[] args)throws Exception {
        //to getting Session object
Session ses=new Configuration().configure().buildSessionFactory().openSession();
Object o=ses.get(Customer.class,new Integer(68));
//typecasting Object class into Customer class
Customer c=(Customer)o;
        System.out.println(""+c.getCnm());
    Vendar v=c.getParent();
    System.out.println(""+v.getVnm());
    System.out.println(""+v.getVid());
        ses.close();
    }
}

No comments: