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;
}
}
分享到:
相关推荐
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
Hibernate连接SQLite配置步骤: 1、添加SQLite库: 1.1、将两个jar包:com.zy.hibernate.dialect.SQLiteDialect.jar、sqlite-jdbc-3.7.2.jar拷贝到“\WEB-INF\lib”文件夹下; 2、配置hibernate: 2.1、将...
Manning出版 Java Persistence With Hibernate 第二版 清晰pdf Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java ...
hibernate
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
hibernate 资料hibernate 资料hibernate 资料hibernate 资料
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate...
Hibernate连表查询 Hibernate连表查询
Java Persistence with Hibernate is the new bible of Hibernate. As a major revision of the popular Hibernate in Action, it builds on the same single example application to introduce and explain the ...
hibernate
Hibernate稳定版(hibernate-release-5.3.23.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
hibernate5 hibernate PDF 讲义 动力 hibernate ssh hibernate5 节点
hibernate annotation hibernate3
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
Hibernate对象的生命周期 Hibernate对象的生命周期 Hibernate对象的生命周期
hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3