Showing posts with label Hibernate One to one example. Show all posts
Showing posts with label Hibernate One to one example. 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();
        }
    }
}