HIBERNATE

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>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">CGMPSP1234</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@//cnbjlscd01.apac.nsroot.net:17206/cnctud</property>
<property name="hibernate.connection.username">CGMPSP</property>

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

<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>

<mapping class="com.citi.base.framwork.orm.hibernate.entity.Address"/>
<mapping class="com.citi.base.framwork.orm.hibernate.entity.Person"/>
<mapping class="com.citi.base.framwork.orm.hibernate.entity.JoinTable_Person"/>


</session-factory>
</hibernate-configuration>
-------------------------------
package com.citi.base.framwork.orm.hibernate;

import org.hibernate.Session;

import com.citi.base.framwork.orm.hibernate.entity.Address;
import com.citi.base.framwork.orm.hibernate.entity.JoinTable_Person;
import com.citi.base.framwork.orm.hibernate.entity.Person;

public class TestHb {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TestHb target = new TestHb();
target.sd_N2one_joinTable();
}

// N person------- 1 address
private void sd_N2one_pk_fk(){

Session session = HibernateUtil.getSession();

HibernateUtil.beginTransaction();

Address address1 = new Address();
address1.setAddressId(0411);
address1.setAddressName("lv shun");

Person person1 = new Person();
person1.setPersonId(101);
person1.setName("father");
person1.setAge(59);
person1.setAddress(address1);
session.persist(person1);

Person person2 = new Person();
person2.setPersonId(102);
person2.setName("mother");
person2.setAge(59);
person2.setAddress(address1);
session.persist(person2);

Person person3 = new Person();
person3.setPersonId(103);
person3.setName("me");
person3.setAge(32);
person3.setAddress(address1);
session.persist(person3);

Address a2 = new Address();
a2.setAddressId(10);
a2.setAddressName("bei jing");

person3.setAddress(a2);

HibernateUtil.commitTransaction();

HibernateUtil.closeSession();

HibernateUtil.closeSessionFacotry();

}

// N person------- 1 address
private void sd_N2one_joinTable(){

Session session = HibernateUtil.getSession();

HibernateUtil.beginTransaction();

Address address1 = new Address();
address1.setAddressId(411);
address1.setAddressName("lv shun");

JoinTable_Person person1 = new JoinTable_Person(101,"father",59);
person1.setAddress(address1);
session.persist(person1);

JoinTable_Person person2 = new JoinTable_Person(102,"mother",58);
person2.setAddress(address1);
session.persist(person2);

JoinTable_Person person3 = new JoinTable_Person(103,"me",32);


Address address2 = new Address();
address2.setAddressId(10);
address2.setAddressName("beijing");
person3.setAddress(address2);

session.persist(person3);

HibernateUtil.commitTransaction();

HibernateUtil.closeSession();

HibernateUtil.closeSessionFacotry();

}


// 1 person -- 1 address
private void sd_one2one(){

Session session = HibernateUtil.getSession();

HibernateUtil.beginTransaction();

Address address1 = new Address();
address1.setAddressId(0411);
address1.setAddressName("lv shun");

Person person1 = new Person(101,"father",59);
person1.setAddress(address1);
session.persist(person1);


Person person2 = new Person(102,"father",58);
person2.setAddress(address1);
session.persist(person2);

Person person3 = new Person(103,"me",32);
person3.setAddress(address1);
session.persist(person3);

Address a2 = new Address();
a2.setAddressId(10);
a2.setAddressName("bei jing");

person3.setAddress(a2);

HibernateUtil.commitTransaction();

HibernateUtil.closeSession();

HibernateUtil.closeSessionFacotry();

}

}

----------
package com.citi.base.framwork.orm.hibernate;

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

public class HibernateUtil {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/**//** Holds a single instance of Session */
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();
/**//** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/**//** The single instance of hibernate SessionFactory */
private static SessionFactory sessionFactory;

/**//**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}

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

/**//**
* Close the single hibernate session factory instance.
*
* @throws HibernateException
*/
public static void closeSessionFacotry() throws HibernateException {

if(sessionFactory!=null){

sessionFactory.close();
}

}

/**//**
* Default constructor.
*/
private HibernateUtil() {
}

public static void beginTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new HibernateException(ex.toString());
}
}

public static void commitTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new HibernateException(ex.toString());
}
}

public static void rollbackTransaction() throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException ex) {
throw new HibernateException(ex.toString());
} finally {
closeSession();
}
}
}

-----
package com.citi.base.framwork.orm.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="TEST_PERSON")
public class Person {
@Id
@Column(name="PERSON_ID")
private int personId = 0;

private String name = null;

private int age = 0;

@ManyToOne(targetEntity=Address.class)
@JoinColumn(name="ADDRESS_ID", nullable=false)
@Cascade(CascadeType.ALL)

private Address address = null;

public Person(){}

public Person(int personId,String name,int age){
this.personId = personId;
this.name = name;
this.age = age;
}


public int getPersonId() {
return personId;
}


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


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


public Address getAddress() {
return address;
}


public void setAddress(Address address) {
this.address = address;
}



}
---
package com.citi.base.framwork.orm.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="TEST_PERSON")
public class JoinTable_Person {
@Id
@Column(name="PERSON_ID")
private int personId = 0;
private String name = null;
private int age = 0;

@ManyToOne(targetEntity=Address.class)
@JoinTable(
name="TEST_PERSON_ADDRESS",
joinColumns =@JoinColumn(name="person_id", referencedColumnName="person_id", unique=true),
inverseJoinColumns=@JoinColumn(name="address_id",referencedColumnName="address_id",unique=true)
)
private Address address = null;

public JoinTable_Person(){}

public JoinTable_Person(int personId,String name,int age){
this.personId = personId;
this.name = name;
this.age = age;
}

public int getPersonId() {
return personId;
}


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


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


public Address getAddress() {
return address;
}


public void setAddress(Address address) {
this.address = address;
}



}
---
package com.citi.base.framwork.orm.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="TEST_ADDRESS")

public class Address {

@Id
@Column(name="ADDRESS_ID")
private int addressId=0;

@Column(name="ADDRESS_NAME")
private String addressName = null;

public Address(){

}

public Address(String p_addressName){
this.addressName = p_addressName;
}

public int getAddressId() {
return addressId;
}

public void setAddressId(int addressId) {
this.addressId = addressId;
}

public String getAddressName() {
return addressName;
}

public void setAddressName(String addressName) {
this.addressName = addressName;
}





}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值