Showing posts with label hibernate mapping. Show all posts
Showing posts with label hibernate mapping. Show all posts

Sunday, January 23, 2011

Hibernate One 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="student.hbm.xml"/>
<mapping resource="address.hbm.xml"/>
       
    </session-factory>


</hibernate-configuration>


student.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="Student" table="STUDENT">
    <id name="sid" column="SID">
        <generator class="increment" />
    </id>
    <property name="snm" column="SNAME" type="java.lang.String"/>
</class>


</hibernate-mapping>


address.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="Address" table="ADDRS">
    <id name="id" column="ID" type="java.lang.Integer">
    <generator class="foreign">
    <param name="property">parent</param>
    </generator>
    </id>
    <property name="hno" column="HNO" type="java.lang.Integer"/>
    <property name="city" column="CITY" type="java.lang.String"/>
   
    <one-to-one name="parent" class="Student" cascade="all"></one-to-one>
</class>


</hibernate-mapping>


Address.java :

/**
 * @author Mallik
 *
 */
public class Address
{
    /*
     * DataMembers Declaration
     */
private Integer id;
private Integer hno;
private String city;
private Student parent;




public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public Integer getHno() {
    return hno;
}
public void setHno(Integer hno) {
    this.hno = hno;
}


public Student getParent() {
    return parent;
}
public void setParent(Student parent) {
    this.parent = parent;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
}


Student.java :


/**
 * @author Mallik
 *
 */


//parent pojo class
public class Student {
    /*
     * DataMembers Declaration
     */
private int sid;
private String snm;


//setters&getters
public int getSid() {
    return sid;
}
public void setSid(int sid) {
    this.sid = sid;
}
public String getSnm() {
    return snm;
}
public void setSnm(String snm) {
    this.snm = snm;
}
}

Test.java :
/**
 * @author Mallik
 *
 */
import java.util.*;
import org.hibernate.cfg.*;
import org.hibernate.*;
//one-to-one Application
public class Test {


        public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("nnnnnnnnnnnnnnn");
        //Session Object creation
        Session ses = new Configuration().configure().buildSessionFactory()
                .openSession();


        Student s = new Student();
       
        s.setSnm("kiran");


        Address a = new Address();
        a.setId(12);
        a.setCity("hyd");
        a.setHno(18);
        a.setParent(s);


        Transaction transaction = null;


        try {
            transaction = ses.beginTransaction();
            ses.save(a);
            transaction.commit();
            System.out.println("okkkkkkkkkkk");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
            if (transaction != null) {
                transaction.rollback();


            }
        } finally
        {
            ses.close();
        }
    }
}

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();
    }
}