- 浏览: 658051 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (609)
- java (139)
- 数据库 (107)
- 微信 (23)
- IT生活 (5)
- web前端 (74)
- SSH (11)
- 设计模式 (12)
- 重要资料 (11)
- 其他 (15)
- java技巧 (23)
- 服务器 (9)
- 2D/GUI (3)
- JAVA3D (2)
- ANT (5)
- Apache项目 (19)
- 数据类型 (10)
- 报表 (3)
- Collections (6)
- SQL/JDBC (15)
- 开发类 (6)
- EJB (6)
- Email (6)
- 文件读写 (2)
- 游戏 (0)
- Flex (2)
- Generic (2)
- HIbernate (12)
- I18N (5)
- Java EE (9)
- java ME (4)
- JDK 6 (8)
- JNDI/LDAP (5)
- JSP (7)
- JSTL (2)
- 正则表达式 (2)
- 安全 (2)
- Struts2 (12)
- Spring (4)
- Web服务 (10)
- Xml (1)
- JavaScript (30)
- AJAX (7)
- 验证 (4)
- 上传下载 (1)
- office办公软件 (1)
- Android (2)
- IOS (0)
- Dubbo (3)
- memcached/redis (1)
- 小程序 (1)
- 微信公众号 (0)
最新评论
-
wf_wangfeng:
怎么我用第一种方法不行呢 alert(document.rea ...
当jsp页面完全加载完成后执行一个js函数 -
Lori_Liu:
有帮助,至少可以解决了目前所遇到的问题!谢谢..
当jsp页面完全加载完成后执行一个js函数 -
starbhhc:
String actionMessage = new Stri ...
Java读取txt文件乱码 -
starbhhc:
Sev7en_jun 写道GOOD
客气,互相交流。。
javaeye论坛规则小测验(答案)--star -
Sev7en_jun:
GOOD
javaeye论坛规则小测验(答案)--star
package nl.one_to_one;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @hibernate.class
* table="passport"
*
*/
public class Passport implements Serializable {
/** identifier field */
private Integer id;
/** nullable persistent field */
private String area;
/** nullable persistent field */
private nl.one_to_one.User user;
/** full constructor */
public Passport(Integer id, String area, nl.one_to_one.User user) {
this.id = id;
this.area = area;
this.user = user;
}
/** default constructor */
public Passport() {
}
/** minimal constructor */
public Passport(Integer id) {
this.id = id;
}
/**
* @hibernate.id
* generator-class="assigned"
* type="java.lang.Integer"
* column="id"
*
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property
* column="area"
* length="100"
*
*/
public String getArea() {
return this.area;
}
public void setArea(String area) {
this.area = area;
}
/**
* @hibernate.one-to-one
* outer-join="auto"
*
*/
public nl.one_to_one.User getUser() {
return this.user;
}
public void setUser(nl.one_to_one.User user) {
this.user = user;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
package nl.one_to_one;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @hibernate.class
* table="user"
*
*/
public class User implements Serializable {
/** identifier field */
private Integer id;
/** nullable persistent field */
private String name;
/** nullable persistent field */
private String pwd;
/** nullable persistent field */
private nl.one_to_one.Passport passport;
/** full constructor */
public User(Integer id, String name, String pwd, nl.one_to_one.Passport passport) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.passport = passport;
}
/** default constructor */
public User() {
}
/** minimal constructor */
public User(Integer id) {
this.id = id;
}
/**
* @hibernate.id
* generator-class="assigned"
* type="java.lang.Integer"
* column="id"
*
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property
* column="name"
* length="100"
*
*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.property
* column="pwd"
* length="100"
*
*/
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/**
* @hibernate.one-to-one
* class="nl.one_to_one.Passport"
* outer-join="auto"
* constrained="true"
*
*/
public nl.one_to_one.Passport getPassport() {
return this.passport;
}
public void setPassport(nl.one_to_one.Passport passport) {
this.passport = passport;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="nl.one_to_one.Passport"
table="passport"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="passport"
</meta>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Integer"
column="id"
</meta>
<generator class="native" />
</id>
<property
name="area"
type="java.lang.String"
column="area"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="area"
length="100"
</meta>
</property>
<!-- Associations -->
<!-- bi-directional one-to-one association to User -->
<one-to-one
name="user"
class="nl.one_to_one.User"
outer-join="auto" cascade="all"
>
<meta attribute="field-description">
@hibernate.one-to-one
outer-join="auto"
</meta>
</one-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="nl.one_to_one.User"
table="user"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="user"
</meta>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Integer"
column="id"
</meta>
<generator class="foreign">
<param name="property">passport</param>
</generator>
</id>
<property
name="name"
type="java.lang.String"
column="name"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="name"
length="100"
</meta>
</property>
<property
name="pwd"
type="java.lang.String"
column="pwd"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="pwd"
length="100"
</meta>
</property>
<!-- Associations -->
<!-- bi-directional one-to-one association to Passport -->
<one-to-one
name="passport"
class="nl.one_to_one.Passport"
outer-join="auto"
constrained="true"
>
<meta attribute="field-description">
@hibernate.one-to-one
class="nl.one_to_one.Passport"
outer-join="auto"
constrained="true"
</meta>
</one-to-one>
</class>
</hibernate-mapping>
测试代码:
package test;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import nl.one_to_one.Passport;
import nl.one_to_one.User;
public class One2One extends TestCase {
protected void tearDown() throws Exception {
super.tearDown();
seesion.close();
}
Session seesion = null;
public void init() {
System.out.println("test.......");
}
protected void setUp() throws Exception {
super.setUp();
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
seesion = factory.openSession();
}
public void testItem() {
try {
Passport passport=new Passport();
passport.setArea("zhangzhou");
User user=new User();
user.setName("cyhgo");
user.setPwd("123456");
passport.setUser(user);
user.setPassport(passport);
Transaction transaction=seesion.beginTransaction();
seesion.save(passport);
seesion.flush();
transaction.commit();
List list=seesion.createQuery("from Passport").list();
Iterator<Passport> it=list.iterator();
System.out.println(list.size());
while(it.hasNext())
{
Passport pass=it.next();
System.out.println(pass.getArea());
User user2=pass.getUser();
System.out.println(user2.getName()+"_"+user2.getPwd());
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
passport 的ID设置为"自动增长"
user的ID设置为外键,关联于passport的ID
在测试的时候必须要注意这两张表之间因为已经存在了一对一的关系,所以我们不能只写
user.setPassport(passport);
而忽略了
passport.setUser(user);
如果不写cascade="all"或者写成cascade="none"的话,即使你写了
user.setPassport(passport);
passport.setUser(user);
也不会发生任何事情,只有passport会被存储。
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @hibernate.class
* table="passport"
*
*/
public class Passport implements Serializable {
/** identifier field */
private Integer id;
/** nullable persistent field */
private String area;
/** nullable persistent field */
private nl.one_to_one.User user;
/** full constructor */
public Passport(Integer id, String area, nl.one_to_one.User user) {
this.id = id;
this.area = area;
this.user = user;
}
/** default constructor */
public Passport() {
}
/** minimal constructor */
public Passport(Integer id) {
this.id = id;
}
/**
* @hibernate.id
* generator-class="assigned"
* type="java.lang.Integer"
* column="id"
*
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property
* column="area"
* length="100"
*
*/
public String getArea() {
return this.area;
}
public void setArea(String area) {
this.area = area;
}
/**
* @hibernate.one-to-one
* outer-join="auto"
*
*/
public nl.one_to_one.User getUser() {
return this.user;
}
public void setUser(nl.one_to_one.User user) {
this.user = user;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
package nl.one_to_one;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @hibernate.class
* table="user"
*
*/
public class User implements Serializable {
/** identifier field */
private Integer id;
/** nullable persistent field */
private String name;
/** nullable persistent field */
private String pwd;
/** nullable persistent field */
private nl.one_to_one.Passport passport;
/** full constructor */
public User(Integer id, String name, String pwd, nl.one_to_one.Passport passport) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.passport = passport;
}
/** default constructor */
public User() {
}
/** minimal constructor */
public User(Integer id) {
this.id = id;
}
/**
* @hibernate.id
* generator-class="assigned"
* type="java.lang.Integer"
* column="id"
*
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property
* column="name"
* length="100"
*
*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.property
* column="pwd"
* length="100"
*
*/
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/**
* @hibernate.one-to-one
* class="nl.one_to_one.Passport"
* outer-join="auto"
* constrained="true"
*
*/
public nl.one_to_one.Passport getPassport() {
return this.passport;
}
public void setPassport(nl.one_to_one.Passport passport) {
this.passport = passport;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="nl.one_to_one.Passport"
table="passport"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="passport"
</meta>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Integer"
column="id"
</meta>
<generator class="native" />
</id>
<property
name="area"
type="java.lang.String"
column="area"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="area"
length="100"
</meta>
</property>
<!-- Associations -->
<!-- bi-directional one-to-one association to User -->
<one-to-one
name="user"
class="nl.one_to_one.User"
outer-join="auto" cascade="all"
>
<meta attribute="field-description">
@hibernate.one-to-one
outer-join="auto"
</meta>
</one-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="nl.one_to_one.User"
table="user"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="user"
</meta>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Integer"
column="id"
</meta>
<generator class="foreign">
<param name="property">passport</param>
</generator>
</id>
<property
name="name"
type="java.lang.String"
column="name"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="name"
length="100"
</meta>
</property>
<property
name="pwd"
type="java.lang.String"
column="pwd"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="pwd"
length="100"
</meta>
</property>
<!-- Associations -->
<!-- bi-directional one-to-one association to Passport -->
<one-to-one
name="passport"
class="nl.one_to_one.Passport"
outer-join="auto"
constrained="true"
>
<meta attribute="field-description">
@hibernate.one-to-one
class="nl.one_to_one.Passport"
outer-join="auto"
constrained="true"
</meta>
</one-to-one>
</class>
</hibernate-mapping>
测试代码:
package test;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import nl.one_to_one.Passport;
import nl.one_to_one.User;
public class One2One extends TestCase {
protected void tearDown() throws Exception {
super.tearDown();
seesion.close();
}
Session seesion = null;
public void init() {
System.out.println("test.......");
}
protected void setUp() throws Exception {
super.setUp();
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
seesion = factory.openSession();
}
public void testItem() {
try {
Passport passport=new Passport();
passport.setArea("zhangzhou");
User user=new User();
user.setName("cyhgo");
user.setPwd("123456");
passport.setUser(user);
user.setPassport(passport);
Transaction transaction=seesion.beginTransaction();
seesion.save(passport);
seesion.flush();
transaction.commit();
List list=seesion.createQuery("from Passport").list();
Iterator<Passport> it=list.iterator();
System.out.println(list.size());
while(it.hasNext())
{
Passport pass=it.next();
System.out.println(pass.getArea());
User user2=pass.getUser();
System.out.println(user2.getName()+"_"+user2.getPwd());
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
passport 的ID设置为"自动增长"
user的ID设置为外键,关联于passport的ID
在测试的时候必须要注意这两张表之间因为已经存在了一对一的关系,所以我们不能只写
user.setPassport(passport);
而忽略了
passport.setUser(user);
如果不写cascade="all"或者写成cascade="none"的话,即使你写了
user.setPassport(passport);
passport.setUser(user);
也不会发生任何事情,只有passport会被存储。
发表评论
-
Hibernate 所有缓存机制详解
2014-12-04 17:00 530Hibernate缓存的作用: Hibernate是 ... -
Hibernate 连接池的三种配置方式:dbcp , c3p0 , proxool
2010-11-09 16:42 1527Hibernate 连接池的三种配置方式:dbcp , c3p ... -
hibernate缓存机制
2010-08-03 09:33 817缓存是介于应用程序和 ... -
在Hibernate配置文件中添加一个自己的驱动类
2010-07-02 16:10 859<project name="hibernat ... -
Hibernate配置文件例子-设置用户名和密码属性
2010-07-02 16:09 896<project name="hibe ... -
Hibernate映射一对一主键单向关联
2010-07-02 16:09 964SQL:car_pk_single表的主键为engine_pk ... -
Hibernate映射一对一外键双向关联
2010-07-02 16:09 845一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一 ... -
Hibernate系列:映射多对多外键双向关联
2010-07-02 16:08 484SQL:class_course是中间表 create ta ... -
Hibernate系列:映射一对多外键双向关联
2010-07-02 16:08 1444一对多关联也可分为单向/双向外键关联,我这里只列出其中的一种, ... -
Hibernate系列:映射一对一连接表单向关联
2010-07-02 16:07 760SQL:C_E_JOIN_SINGLE为中间表 create ... -
hibernate bag 集合映射
2010-07-02 16:07 699Person.java package com.aabnn. ...
相关推荐
"Hibernate one-to-many / many-to-one关系映射"是两个基本的关系类型,用于表示实体间的关联。在这个主题中,我们将深入探讨这两种关系映射的原理、配置和实际应用。 一、一对一(one-to-one)与一对多(one-to-...
在一对一(One-to-One)主键关联关系中,两个实体类之间存在唯一对应的关系,通常一个实体对应另一个实体的一个实例,这种关系可以通过共享主键来实现。 在Hibernate中,一对一的主键关联关系可以通过以下几种方式...
在深入探讨Hibernate中的一对一(One-to-One)主键双线关联机制之前,我们首先需要理解几个核心概念:Hibernate框架、实体关系映射以及主键关联的基本原理。Hibernate是Java平台下的一款开源对象关系映射(ORM)框架...
在Java的持久化框架Hibernate中,一对一(One-to-One)关联映射是常见的关系映射类型之一。它用于表示两个实体之间一对一的关联关系,即一个实体对应另一个实体的唯一实例。下面将详细介绍Hibernate中如何进行一对一...
在这个场景中,“hibernate使用主键关联的一对一关系”指的是通过共享主键来实现两个实体类之间的这种一对一绑定。下面我们将深入探讨这一主题。 首先,我们需要理解一对一关系的概念。在数据库中,一对一关系意味...
在Java的持久化框架Hibernate中,一对一(One-to-One)关联是一种常见的关系映射方式,它表示两个实体之间存在唯一的对应关系。这种关系通常出现在两个实体之间具有唯一性的关联,例如一个人与他的护照,或者一个...
在Java的持久化框架Hibernate中,一对一(One-to-One)关联是对象关系映射(ORM)中的一个重要概念,它表示两个实体类之间的一种一对一的关系。在这种关系中,一个实体类的实例对应另一个实体类的唯一实例。在...
"一对一(One-to-One)"外键关联是Hibernate支持的一种关系映射类型,用于描述两个实体之间一对一的关联关系。这种关系通常存在于具有唯一性的实体之间,例如一个人只有一个身份证,或者一个员工只有一个办公桌。 *...
本篇文章将全面探讨Hibernate的关联映射学习,包括一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)以及多对多(Many-to-Many)四种关系映射。 首先,一对一关联映射(One-to-One)是两个实体...
在Java的持久化框架Hibernate中,一对一(One-to-One)映射是一种常见关系映射,用于表示两个实体之间一对一的关联。本篇文章将详细探讨Hibernate中实现一对一映射的两种方式,并结合源码分析其原理。 一、主键外键...
在Java的持久化框架Hibernate中,一对一(One-to-One)关联关系是常见的一种实体关系映射。本主题将深入探讨如何使用Hibernate通过主键来实现一对一的单向关联关系,并提供相关的源码分析。 首先,我们需要理解一对...
-- 用来映射关联PO,必须为one-to-one元素增加constrained="true"属性 --> <one-to-one name="address" constrained="true"/> </hibernate-mapping> ``` --- ##### 3. 一对一连接表单向关联 在一对一连接表单...
在这个XML配置中,`<one-to-one>`元素表示一对一的关联,`class`属性是关联的实体类,`foreign-key`属性用于指定外键的名称。 在实际使用时,我们可以通过Hibernate Session来操作这两个实体,如下所示: ```java ...
标题“hb-04-one-to-many-uni”和描述中的信息看似简洁,但结合标签“Java”,我们可以推测这是一个关于Java编程中的一个特定主题——一对一(One-to-One)、一对多(One-to-Many)关系的统一处理(uni)的示例项目...
这里我们重点关注一对一(One-to-One)、一对多(One-to-Many)和多对多(Many-to-Many)关系。 1. 一对一关系(One-to-One): - 主键关联:一个实体对象对应另一个实体对象的一个实例,通常通过主键共享。例如,...
在数据表之间,有一种常见的关联关系是一对一(One-to-One),即一个实体对应另一个实体的唯一实例。`hibernate_one_to_one`标签表示我们将探讨的是如何在Hibernate中实现一对一的映射关系。 **二、一对一映射的两...
在Java的持久化框架Hibernate中,一对一(One-to-One)主键映射是一种常见的关联关系。这种映射方式使得两个实体类共享同一个主键,从而建立一对一的关系。在这个实例中,我们将深入探讨Hibernate一对一主键映射的...