`

one to one 主键关联

阅读更多
<?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.Person" table="t_person">
		<id name="id">
			<generator class="foreign">
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name"/>
		<one-to-one name="idCard" constrained="true"/>
	</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.IdCard" table="t_idcard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="cardNo"/>
		<one-to-one name="person"/>
	</class>
</hibernate-mapping>

 

 

 

 

 

 

单向

 

hibernate一对一主键关联映射(单向关联Person---->IdCard)

一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建

具体映射:

 <id name="id">
  <!-- person的主键来源idCard,也就是共享idCard的主键 -->
  <generator class="foreign">
   <param name="property">idCard</param>
  </generator>
 </id>
 <property name="name"/>
 <!-- one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,
 constrained="true", 表明当前主键上存在一个约束,person的主键作为外键参照了idCard 
  -->
 <one-to-one name="idCard" constrained="true"/>

<?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.yelang.hibernate.IdCard" table="t_idcard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="cardNo"/>
	</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.Person" table="t_person">
		<id name="id">
			<generator class="foreign">
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name"/>
		<one-to-one name="idCard" constrained="true"/>
	</class>
</hibernate-mapping>



 

分享到:
评论

相关推荐

    Hibernate学习要点_one2one 一对一主键双线关联

    Hibernate学习要点:一对一主键关联双向

    Hibernate学习笔记

    020 &lt;one-to-one&gt;、&lt;many-to-one&gt;单端关联上的 lazy(懒加载)属性 021 继承关联映射 022 component(组件)关联映射 023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形...

    使用@IdClass创建联合主键,并实现外部关联

    使用@IdClass创建联合主键,并实现外部关联

    Java的Hibernate框架中的双向主键关联与双向外键关联

    Hibernate想要实现双向的关联就必须在映射文件的两端同时配置&lt;one&gt;,另外还要在主映射的一端采用foreign外键关联属性,下面我们就一起来看一下Java的Hibernate框架中的双向主键关联与双向外键关联方法:

    Hibernate one2one_pk_1

    Hibernate学习要点:一对一主键关联双向

    Hibernate_Annotation关联映射

    一对一(One-To-One) 使用@OneToOne注解建立实体Bean之间的一对一关联。一对一关联有三种情况:(1).关联的实体都共享同样的主键,(2).其中一个实体通过外键关联到另一个实体的主键(注意要模拟一对一关联必须在外键列...

    unidirectional-one2one-sharedprimarykey:该存储库正在存储有关Hibernate映射示例的源代码-共享主键上的单向一对一关联-one source code

    单向一对一共享主键 该存储库正在存储有关Hibernate映射示例的源代码-共享主键上的单向一对一关联

    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 框架详解

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

    hibernate3.04中文文档.chm

    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教程

    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 中文 html 帮助文档

    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 参考文档

    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 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.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参考文档

    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学习笔记

    hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person----&gt;IdCard) 10 hibernate一对一...

    hibernate annotation帮助文档

    一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...

    hibernate annotation 中文文档

    一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...

    Hibernate Annotations 中文文档

    一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...

Global site tag (gtag.js) - Google Analytics