`
zhangjingqiang
  • 浏览: 72039 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

导入Hibernate

    博客分类:
  • Java
阅读更多

src\chap06app:

Main.java

package chap06app;

import java.util.List;

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


public class Main{
	
	public static void main( String[] args ){
		
		SessionFactory sf = new AnnotationConfiguration().
							configure().buildSessionFactory();
		Session session = sf.openSession();
		
		Transaction tx = session.beginTransaction();
		try{
			// t_prefにTokyoとChibaがすでに入っているものとする。
			// Tokyoを見つける
			List<Pref> tokyoList = session.createQuery( "from Pref pref where pref.pref like 'Tokyo'" ).list();
			Pref tokyoPref;
			if( tokyoList.size() == 1 ){
				tokyoPref = (Pref)tokyoList.get( 0 );
			}else{
				throw new UnsupportedOperationException( "Tokyo not found!");
			}
			
			System.out.println( "tokyoPref id: " + tokyoPref.getId() );
			System.out.println( "TokyoPref pref: " + tokyoPref.getPref() );
			
			Address okadaAddr = new Address();
			okadaAddr.setName( "Okada" );
			okadaAddr.setPref( tokyoPref );
			okadaAddr.setAddress( "Edogawaku 1-2-3" );
			
			session.save( okadaAddr );
			
			tx.commit();
		}catch( HibernateException he ){
			if( tx != null ){ tx.rollback(); }	
		}finally{
			session.close();
			sf.close();
		}
	}
}

Pref.java

package chap06app;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "t_pref", schema = "public")
public class Pref implements java.io.Serializable {

	private int id;
	private String pref;
	private Set<Address> TAddresses = new HashSet<Address>(0);

	public Pref() {
	}

	public Pref(int id, String pref) {
		this.id = id;
		this.pref = pref;
	}

	public Pref(int id, String pref, Set<Address> TAddresses) {
		this.id = id;
		this.pref = pref;
		this.TAddresses = TAddresses;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "pref", nullable = false)
	public String getPref() {
		return this.pref;
	}

	public void setPref(String pref) {
		this.pref = pref;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pref")
	public Set<Address> getTAddresses() {
		return this.TAddresses;
	}

	public void setTAddresses(Set<Address> TAddresses) {
		this.TAddresses = TAddresses;
	}

}

Address.java

package chap06app;

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

@Entity
@Table(name = "t_address2", schema = "public")
public class Address implements java.io.Serializable {

	private int id;
	private Pref pref;
	private String name;
	private String address;

	public Address() {
	}

	public Address(int id, Pref pref, String name, String address) {
		this.id = id;
		this.pref = pref;
		this.name = name;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pref_id_fk", nullable = false)
	public Pref getPref() {
		return this.pref;
	}

	public void setPref(Pref pref) {
		this.pref = pref;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

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

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

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

}

src\out:

TAddress.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

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

/**
 * TAddress generated by hbm2java
 */
@Entity
@Table(name = "t_address", schema = "public")
public class TAddress implements java.io.Serializable {

	private int id;
	private String name;
	private int pref;
	private String address;

	public TAddress() {
	}

	public TAddress(int id, String name, int pref, String address) {
		this.id = id;
		this.name = name;
		this.pref = pref;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

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

	@Column(name = "pref", nullable = false)
	public int getPref() {
		return this.pref;
	}

	public void setPref(int pref) {
		this.pref = pref;
	}

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

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

}

TAddress2.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

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

/**
 * TAddress2 generated by hbm2java
 */
@Entity
@Table(name = "t_address2", schema = "public")
public class TAddress2 implements java.io.Serializable {

	private int id;
	private TPref TPref;
	private String name;
	private String address;

	public TAddress2() {
	}

	public TAddress2(int id, TPref TPref, String name, String address) {
		this.id = id;
		this.TPref = TPref;
		this.name = name;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pref_id_fk", nullable = false)
	public TPref getTPref() {
		return this.TPref;
	}

	public void setTPref(TPref TPref) {
		this.TPref = TPref;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

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

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

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

}

TPref.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * TPref generated by hbm2java
 */
@Entity
@Table(name = "t_pref", schema = "public")
public class TPref implements java.io.Serializable {

	private int id;
	private String pref;
	private Set<TAddress2> TAddress2s = new HashSet<TAddress2>(0);

	public TPref() {
	}

	public TPref(int id, String pref) {
		this.id = id;
		this.pref = pref;
	}

	public TPref(int id, String pref, Set<TAddress2> TAddress2s) {
		this.id = id;
		this.pref = pref;
		this.TAddress2s = TAddress2s;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "pref", nullable = false)
	public String getPref() {
		return this.pref;
	}

	public void setPref(String pref) {
		this.pref = pref;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TPref")
	public Set<TAddress2> getTAddress2s() {
		return this.TAddress2s;
	}

	public void setTAddress2s(Set<TAddress2> TAddress2s) {
		this.TAddress2s = TAddress2s;
	}

}

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.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">okada</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Chap04DB</property>
        <property name="hibernate.connection.username">okada</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="TAddress2" />
        <mapping class="TAddress" />
        <mapping class="TPref" />
    </session-factory>
</hibernate-configuration>

src:

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">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">okada</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Chap04DB</property>
        <property name="hibernate.connection.username">okada</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>

		<mapping class="chap06app.Address"/>
        <mapping class="chap06app.Pref"/>
    </session-factory>
</hibernate-configuration>

代码来自日本的技术图书http://www.shuwasystem.co.jp/products/7980html/2197.html

分享到:
评论

相关推荐

    hibernate 源码直接导入Eclipse

    Hibernate 是一个著名的开源...总之,导入Hibernate源码到Eclipse中,不仅有助于我们深入理解ORM的工作原理,还能提升我们的数据库操作和设计能力。通过实际操作和调试,我们可以更有效地学习和掌握这个强大的框架。

    Hibernate入门案例源码

    在Eclipse和MyEclipse中,你可以通过右键菜单创建新项目,然后导入Hibernate所需的库,配置好项目结构后,就可以运行案例代码,查看数据库操作的效果。这将帮助你理解Hibernate的工作原理,例如怎样执行HQL...

    ssh(sevrlet+spring+hibernate)整合所需jar包

    6. hibernate:这个目录可能包含Hibernate的核心库和相关的JAR文件,如hibernate-core.jar、hibernate-entitymanager.jar等,以及Hibernate的特定模块,如Hibernate Validator用于校验实体对象。 7. spring:Spring...

    webservice整合了hibernate

    【描述】"webservice整合了hibernate,导入进去直接就能运行"意味着开发者已经完成了一个预配置的项目,其中包含了必要的配置文件、依赖库以及相关的代码实现。用户只需将这个项目导入到开发环境中,如Eclipse或...

    eclipse_hibernate3.3IDE插件

    **Eclipse Hibernate 3.3 IDE插件** Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发者将数据库操作转化为对Java对象的操作,从而简化了数据访问层的编程。这款Eclipse插件是为了更好地集成...

    hibernate 重点知识原创

    在开始使用 Hibernate 之前,需要进行环境配置,主要包括导入 Hibernate 相关的 JAR 包,如 Hibernate 的核心库、JDBC 驱动和单元测试库。配置文件 `hibernate.cfg.xml` 是 Hibernate 的心脏,它包含了数据库连接...

    .hibernate 框架介绍

    - 导入Hibernate所需的依赖jar包,包括Hibernate自身提供的jar包以及数据库驱动jar包。 - 编写Hibernate相关配置文件,包括映射配置文件和核心配置文件Hibernate.cfg.xml。 Hibernate的工作原理涉及配置文件的读取...

    Hibernate知识点总结

    1. **导入Hibernate的Jar包**:这是使用Hibernate的前提条件。 2. **创建Hibernate配置文件**:通常命名为`hibernate.cfg.xml`,此文件用于配置数据库连接信息和其他全局设置。 3. **创建持久化类**:持久化类代表...

    hibernate教程

    创建一个名为`HibernateDemo`的Java工程,并导入Hibernate所需的jar包,包括核心jar包`hibernate3.jar`以及所有位于`lib/required`目录下的jar包。另外,由于涉及到MySQL数据库操作,还需要导入MySQL驱动包。如果你...

    hibernate-distribution-3.6.0.Final手册

    hibernate-distribution-3.6.0.Final手册

    Hibernate快速入门教程

    创建一个名为`HibernateDemo`的Java工程,然后导入Hibernate的核心库,包括`hibernate3.jar`以及`lib`目录下的所有jar包。如果使用最新版本的Hibernate,可能需要7个基本的jar包。同时,由于涉及到数据库操作,还...

    hibernate搭建用到的jar包

    在Java开发中,Hibernate是一个非常流行的ORM(对象关系映射)框架,它简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据。本篇将详细解释`hibernate搭建用到的jar包`,以及它们在Hibernate框架中的...

    Spring+Hibernate+Strust 整合开发步骤

    在导入Hibernate时,选择Hibernate的所有包,并将其复制到工程目录。在配置阶段,选择让Spring管理Hibernate的配置文件,即指定`applicationContext.xml`文件,并为SessionFactory指定一个ID。 6. **创建Hibernate...

    Hibernate依赖jar包

    5. **导入Hibernate工程**:在Java项目中使用Hibernate,需要将这些jar包添加到项目的类路径中,确保编译和运行时能够找到相关的类和方法。这通常可以通过构建工具如Maven或Gradle来管理依赖,或者手动将jar包放入...

    Hibernate入门

    - **导入Hibernate的jar包**:包括Hibernate核心库及其他依赖库。 - **导入日志jar包**:如Log4j或SLF4J等,用于输出日志信息。 - **导入数据库驱动jar包**:如MySQL的驱动程序。 - **实体类创建**: - 在...

    Hibernate自动生成配置文件

    5. **导入Hibernate**:在MyEclipse中,通过工程菜单下的“HibernateCapabilities”选项,导入Hibernate框架,选择数据库连接并完成必要的配置。 6. **用Hibernate自动生成配置文件**:选择数据库中的目标表,如`t_...

    Hibernate操作持久化对象

    1. 导入Hibernate库:将所需的Hibernate JAR包添加到项目的类路径中。 2. 配置hibernate.cfg.xml:设置数据库连接信息,如URL、用户名、密码、驱动类等。 3. 编写实体类:表示数据库中的表,使用注解或XML映射文件...

    Hibernate PDf

    1. **环境搭建**:首先需要配置好JDBC驱动,建立数据库连接,并导入Hibernate相关的jar包。 2. **配置文件**:配置Hibernate的`hibernate.cfg.xml`文件,指定数据库连接信息及其它设置。 3. **实体类设计**:设计...

    Hibernate入门笔记

    - **Jar包导入**:在工程中导入Hibernate的jar包至关重要。这包括`hibernate3.jar`,这是Hibernate的核心库,以及`lib\required`目录下的所有jar包,这些包提供了Hibernate运行所需的额外功能。如果使用的是最新版本...

Global site tag (gtag.js) - Google Analytics