Thursday, February 18, 2010

Hibernate - OneToOne

***************************************************************************************
Tables
***************************************************************************************
create table "SCOTT"."PERSON"(
"PERSON_ID" NUMBER(10) not null,
"PERSON_NAME" VARCHAR2(20),
constraint "PK_PERSON" primary key ("PERSON_ID")
);

create unique index "PK_PERSON" on "SCOTT"."PERSON"("PERSON_ID");

create table "SCOTT"."COMPUTER"(
"COM_ID" NUMBER(10) not null,
"COMPUTER_NAME" VARCHAR2(20),
"PROCESSOR" VARCHAR2(20),
"PERSON_ID" NUMBER(10) unique,
constraint "PK_COMPUTER" primary key ("COM_ID")
);

alter table "SCOTT"."COMPUTER"
add constraint "FK_PERSON_ID"
foreign key ("PERSON_ID")
references "SCOTT"."PERSON"("PERSON_ID");
create unique index "PK_COMPUTER" on "SCOTT"."COMPUTER"("COM_ID");
create unique index "UNIQUE_PERID" on "SCOTT"."COMPUTER"("PERSON_ID");
***************************************************************************************
Hibernate Factory Class
------------------------
package factory;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

private HibernateSessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the SessionFactory if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}

}

***************************************************************************************
Pojo Classes
------------
package model;
// default package



/**
* Computer generated by MyEclipse - Hibernate Tools
*/

public class Computer implements java.io.Serializable {


// Fields

private Long comId;
private Person person;
private String computerName;
private String processor;


// Constructors

/** default constructor */
public Computer() {
}


/** full constructor */
public Computer(Person person, String computerName, String processor) {
this.person = person;
this.computerName = computerName;
this.processor = processor;
}


// Property accessors

public Long getComId() {
return this.comId;
}

public void setComId(Long comId) {
this.comId = comId;
}

public Person getPerson() {
return this.person;
}

public void setPerson(Person person) {
this.person = person;
}

public String getComputerName() {
return this.computerName;
}

public void setComputerName(String computerName) {
this.computerName = computerName;
}

public String getProcessor() {
return this.processor;
}

public void setProcessor(String processor) {
this.processor = processor;
}
}



package model;
// default package

import java.util.HashSet;
import java.util.Set;


/**
* Person generated by MyEclipse - Hibernate Tools
*/

public class Person implements java.io.Serializable {


// Fields

private Long personId;
private String personName;
private Set computers = new HashSet(0);


// Constructors

/** default constructor */
public Person() {
}


/** full constructor */
public Person(String personName, Set computers) {
this.personName = personName;
this.computers = computers;
}


// Property accessors

public Long getPersonId() {
return this.personId;
}

public void setPersonId(Long personId) {
this.personId = personId;
}

public String getPersonName() {
return this.personName;
}

public void setPersonName(String personName) {
this.personName = personName;
}

public Set getComputers() {
return this.computers;
}

public void setComputers(Set computers) {
this.computers = computers;
}
}
***************************************************************************************
Mapping Files
--------------
***************************************************************************************
DAO Class
-----------
package dao;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;

import factory.HibernateSessionFactory;

public class OneToOneDAO {
public Object saveComputer(Class clazz, Object object) throws HibernateException {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Serializable id = session.save(object);
tx.commit();
Object returnObj = session.get(clazz, id, LockMode.UPGRADE);
return returnObj;
}
}
***************************************************************************************
Test Class
----------
package test;

import java.util.HashSet;
import java.util.Set;

import dao.OneToOneDAO;

import model.Computer;
import model.Person;

public class TestOneToOne {

/**
* @param args
*/
public static void main(String[] args) {
OneToOneDAO oneToOneDAO = new OneToOneDAO();

Computer computer = new Computer();
computer.setComputerName("Gr_Computer");
computer.setProcessor("Intel Duo Core");

Person person = new Person();
person.setPersonName("Gangadhar");

/*Person person1 = new Person();
person1.setPersonName("Rao");*/

computer.setPerson(person);
// Here in Computer class you can able to set only one person object, you can not set more than one.
/*computer.setPerson(person1);*/

Set setOfComputers = new HashSet();
setOfComputers.add(computer);

person.setComputers(setOfComputers);
/*person1.setComputers(setOfComputers);*/

oneToOneDAO.saveComputer(Computer.class, computer);
}
}
***************************************************************************************

No comments:

Post a Comment