Saturday, April 21, 2012

Hibernate One to One Mapping Example


package com.hibernate.venkat;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Person {


private int personId;
private String personName;

private PersonDetail pdetail;

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="pdetal_FK")
public PersonDetail getPdetail() {
return pdetail;
}
public void setPdetail(PersonDetail pdetail) {
this.pdetail = pdetail;
}
@Id
@GeneratedValue
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}



}
---------------------------------------------------------------------------------------------

package com.hibernate.venkat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class PersonDetail {

private int personDetailId;
private String zipcode;
private String job;
private double income;
@Id
@GeneratedValue
@Column(name="detail_pk")
public int getPersonDetailId() {
return personDetailId;
}
public void setPersonDetailId(int personDetailId) {
this.personDetailId = personDetailId;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
}
---------------------------------------------------------------------------------------------

/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestPerson {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Person.class);
config.addAnnotatedClass(PersonDetail.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
PersonDetail pd=new PersonDetail();
pd.setIncome(99756.89);
pd.setJob("Developer");
pd.setZipcode("30328");
Person venkat=new Person();
venkat.setPersonName("Venkat");
venkat.setPdetail(pd);
s.save(venkat);
//s.save(pd); no need if we set cascadetype
s.getTransaction().commit();

}

}
---------------------------------------------------------------------------------------------
<?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="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

No comments:

Post a Comment