`

7.hibernate一对一双向外键关联,xml映射

阅读更多
stuidcard表结构:

student表结构:



package com.bjsxt.hibernate;

public class Student {
	
	private int id;
	private String name;
	
	private int age;
	private String sex;
	private boolean good;
	private StuIdCard stuIdCard;
	public StuIdCard getStuIdCard() {
		return stuIdCard;
	}
	public void setStuIdCard(StuIdCard stuIdCard) {
		this.stuIdCard = stuIdCard;
	}
	public boolean isGood() {
		return good;
	}
	public void setGood(boolean good) {
		this.good = good;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	
}



package com.bjsxt.hibernate;

public class StuIdCard {
	private int id;
	private String num;
	private Student student;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	
}



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.StuIdCard">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="num"/>
                  <!-- 外键在这边, 所以映射成many-to-one 用唯一键约束 -->
		<many-to-one name="student" column="studentId" unique="true"></many-to-one>
    </class>
	
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<property name="age" />
		<property name="sex" />
		<property name="good" type="yes_no"></property>
		<one-to-one name="stuIdCard" property-ref="student"></one-to-one>
    </class>
	
</hibernate-mapping>
分享到:
评论

相关推荐

    hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向

    Hibernate 一对一唯一外键关联映射详解 Hibernate是一种流行的Java持久层框架,提供了多种关联映射方式,其中一对一唯一外键关联映射是一种常见的关联方式。在本文中,我们将详细介绍Hibernate一对一唯一外键关联...

    hibernate_reference中文文档.pdf

    1.1. 第一部分 - 第一个 Hibernate 应用程序 ................................. 1 1.1.1. 设置 ............................................................ 1 1.1.2. 第一个 class .............................

    HibernateAPI中文版.chm

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联...

    Hibernate Annotations 中文文档

    2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4...

    hibernate annotation 中文文档

    2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. ...

    Hibernate+中文文档

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联...

    hibernate3.2中文文档(chm格式)

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联...

    Hibernate 中文 html 帮助文档

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联生命...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联...

    Hibernate中文详细学习文档

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联...

    hibernate3.04中文文档.chm

    20.1.7. 使用延迟属性抓取(Using lazy property fetching) 20.2. 二级缓存(The Second Level Cache) 20.2.1. 缓存映射(Cache mappings) 20.2.2. 策略:只读缓存(Strategy: read only) 20.2.3. 策略:...

    Hibernate教程

    20.1.7. 使用延迟属性抓取(Using lazy property fetching) 20.2. 二级缓存(The Second Level Cache) 20.2.1. 缓存映射(Cache mappings) 20.2.2. 策略:只读缓存(Strategy: read only) 20.2.3. 策略:读/...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    Hibernate注释大全收藏

    这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate3的帮助文档

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    最全Hibernate 参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    Hibernate参考文档

    20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联生命...

    Hibernate3+中文参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    hibernate的中所有关系映射(分别用xml和jpa实现)

    hibernate的中关系映射分为一对一、一对多、多对一、多对多,其中有单双向之分。而一对一还有主键和外键区分。一共是九种。分别用xml配置文件和annotation实现。annotation的版本没有写测试程序,见谅。

Global site tag (gtag.js) - Google Analytics