`

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;
}





}
分享到:
评论

相关推荐

    Hibernate 配置各种数据库

    Hibernate 配置各种数据库 Hibernate 是一个基于 Java 的持久层框架,提供了一个抽象的数据访问层,能够与多种数据库进行集成。在 Hibernate 的配置文件中,我们可以配置不同的数据库连接,包括驱动程序、URL 等...

    hibernate jar包:hibernate-commons-annotations-4.0.1.Final.jar等

    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-...

    Hibernate入门到精通

    "Hibernate入门到精通" Hibernate 是一个基于Java的ORM(Object-Relational Mapping,对象关系映射)框架,它提供了一种简洁高效的方式来访问和操作关系数据库。下面是 Hibernate 的主要知识点: Hibernate 简介 ...

    最新版本的Struts2+Spring4+Hibernate4框架整合

    项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...

    hibernate学习资料大全

    【hibernate学习资料大全】 Hibernate 是一个开源的对象关系映射(ORM)框架,它极大地简化了Java应用程序对数据库的操作。这个压缩包包含了丰富的Hibernate学习资源,旨在帮助开发者深入理解和熟练掌握这一强大的...

    Hibernate-extensions 完整安装包

    《Hibernate-Extensions全面指南》 Hibernate,作为Java领域中的一款著名对象关系映射(ORM)框架,极大地简化了数据库操作。然而,为了满足更复杂的业务需求,Hibernate还提供了丰富的扩展功能,这就是我们今天要...

    hibernate-release-4.1.4

    【描述】中的"hibernate的jar包"指的是Hibernate框架的运行库文件,这些JAR文件包含了Hibernate的所有核心API、实现和依赖库,如Hibernate Commons Annotations、Hibernate EntityManager、Hibernate Core等。...

    hibernate基础jar包

    Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发者使用面向对象的方式来操作数据库,极大地简化了数据访问层的编程工作。这个压缩包包含了Hibernate的基础jar包,这些jar文件是开发Hibernate应用所...

    HibernateTools-3.2.4

    HibernateTools是Java开发人员在使用Hibernate ORM框架时的有力辅助工具集,主要目的是为了提高开发效率,简化数据库操作。在HibernateTools 3.2.4版本中,它包含了一系列的特性与插件,以支持更便捷地进行对象关系...

    kingbaseV8 hibernate jdbc 驱动

    在Java开发环境中,与KingbaseV8数据库进行交互通常会用到Hibernate框架和JDBC驱动。 Hibernate是一个优秀的对象关系映射(ORM)框架,它简化了Java应用程序对数据库的操作,通过将Java对象与数据库表进行映射,...

    hibernate3全部jar包:hibernate3.jar.zip 下载

    Hibernate3 是一个强大的Java持久化框架,它允许开发者将数据库操作与业务逻辑解耦,使得应用程序的开发更为简便。这个“hibernate3全部jar包:hibernate3.jar.zip”包含了所有必要的库文件,方便用户一次性下载并...

    Hibernate3的依赖包

    Hibernate3是一个广泛使用的Java对象关系映射(ORM)框架,它允许开发者用面向对象的方式处理数据库操作,极大地简化了Java应用程序与数据库之间的交互。在这个"Hibernate3的依赖包"中,包含了运行Hibernate3应用...

    hibernate和MySQL的jar

    标题中的“hibernate和MySQL的jar”指的是Hibernate ORM框架与MySQL数据库之间的连接库。Hibernate是一种流行的Java对象关系映射(ORM)工具,它允许开发者使用面向对象的编程方式来操作数据库,而无需直接编写SQL...

    hibernate4.3完整jar包

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...

    Hibernate 中文api 等学习资料

    标题"Hibernate 中文api 等学习资料"暗示了这是一组针对Hibernate ORM框架的中文学习资源,包括API文档和其他指南,旨在帮助用户更好地理解和使用Hibernate。 描述中的"hibernate orm框架api中文文档,学习资料,...

    hibernate.5.1.0.jar全部

    包含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入门jar包

    Hibernate是一款强大的Java持久化框架,它简化了数据库与Java对象之间的交互,使开发者可以更加专注于业务逻辑而不是数据访问层的实现。本压缩包提供的是Hibernate入门所需的jar包,包括了Hibernate的核心库以及与之...

    hibernate3必要jar包

    Hibernate3 是一个非常重要的Java持久化框架,它允许开发者将对象模型与数据库关系模型进行映射,从而简化了数据存取的过程。这个压缩包“hibernate3必要jar包”显然是针对Hibernate3版本的,已经去除了冗余的库文件...

    hibernate_3.2官方开发包

    Hibernate 是一个著名的开源对象关系映射(ORM)框架,它为Java开发者提供了便捷的数据持久化方式,使得在Java应用程序中操作数据库变得更加简单。这个“hibernate_3.2官方开发包”包含了所有你需要开发基于...

    hibernate需要的10个jar

    在Java开发领域,Hibernate是一个非常重要的对象关系映射(ORM)框架,它简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据。当涉及到Hibernate项目时,为了使其正常运行,通常需要一系列的JAR(Java ...

Global site tag (gtag.js) - Google Analytics