(**)hibernate中基于主键和基于外键的one-to-one
来源:互联网 发布:汽配数据服务商 编辑:程序博客网 时间:2024/05/22 17:25
基于主键的单向 1-1
基于主键关联的持久化类不能拥有自己的主键生成器,它的主键由关联类负责生成。
one-to-one:必须为one-to-one元素增加constrained="true"属性,表明该类的主键由关联类生成
【要点】Person - IdCard
<id name=”id”>
<generator class=”foreign”><param name=”property”>idCard</param></generator>
<id>
<one-to-one name=”idCard” constrained=”true”/>
基于外键的单向1-1
和<many-to-one>N-1相同,多添加一个unique="true" 属性,用以表示N的一端必须唯一,加了唯一约束,即成了1-1
【举例】
<one-to-one name=”idCard” property-ref=“person”/>
<many-to-one name=”person” column=”person_id” unique=”true” not-null=”true”/>
2、基于外键的1-1
在此处跌倒多次,必须注意了啊!!
基于主键关联的持久化类不能拥有自己的主键生成器,它的主键由关联类负责生成。
one-to-one:必须为one-to-one元素增加constrained="true"属性,表明该类的主键由关联类生成
【要点】Person - IdCard
<id name=”id”>
<generator class=”foreign”><param name=”property”>idCard</param></generator>
<id>
<one-to-one name=”idCard” constrained=”true”/>
基于外键的单向1-1
和<many-to-one>N-1相同,多添加一个unique="true" 属性,用以表示N的一端必须唯一,加了唯一约束,即成了1-1
【举例】
<one-to-one name=”idCard” property-ref=“person”/>
<many-to-one name=”person” column=”person_id” unique=”true” not-null=”true”/>
property-ref:用于指定关联类的一个属性,这个属性将会和本外键相对应
举例
1、基于主键的单向1-1
Person:PK id 其他name、age
id_card:PK,FK id 其他useful_life
IdCard.hbm.xml
<hibernate-mapping> <class name="cn.itcast.hibernate.domain.IdCard" table="id_card" catalog="test"> <id name="id" type="java.lang.Integer"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" class="cn.itcast.hibernate.domain.Person" constrained="true"></one-to-one> <property name="usefulLife" type="java.util.Date"> <column name="useful_life" length="10" not-null="true" /> </property> </class></hibernate-mapping>
2、基于外键的1-1
Person:PK id 其他name、age
id_card:PK id FK useful_life person_id
IdCard.hbm.xml
<hibernate-mapping> <class name="cn.itcast.hibernate.domain.IdCard" table="id_card" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native"></generator> </id> <many-to-one name="person" class="cn.itcast.hibernate.domain.Person" update="false" insert="false" fetch="select"> <column name="id" not-null="true" unique="true" /> </many-to-one> <property name="usefulLife" type="java.util.Date"> <column name="useful_life" length="10" not-null="true" /> </property> </class></hibernate-mapping>
在此处跌倒多次,必须注意了啊!!
- (**)hibernate中基于主键和基于外键的one-to-one
- Hibernate的基于主键的one-to-one
- Hibernate的基于外健的one-to-one
- one to one 基于注解配置的共享主键的操作说明
- hibernate------one-to-one(一个表的主键由另一个表的主键决定)
- Hibernate一对一 主键关联映射(one-to-one)
- Hibernate一对一 主键关联映射(one-to-one)
- hibernate annotation one to one 共享主键
- Hibernate中one-to-one
- Hibernate.one-to-one外键策略
- hibernate one-to-one外键关联
- hibernate------one-to-one(通过外键方式实现)
- hibernate------one-to-one(通过外键方式实现)
- 关于hibernate中双向外键关联one-to-one的property-ref=的问题
- 关于hibernate中双向外键关联one-to-one的property-ref=的问题
- 关于hibernate中双向外键关联one-to-one的property-ref=的问题
- hibernate的one-to-many和many-to-one实例
- 关于Hibernate中many-to-one和one-to-many的一些理解
- 第五章 Web服务器
- Java 数据类型转换【学习备份】
- 服务器端Server-U忘记密码的解决办法
- HDU1027Ignatius and the Princess II(排序)
- MySQL不需循环update多条记录
- (**)hibernate中基于主键和基于外键的one-to-one
- 两日期之间的天数
- 命令行下执行SQL sever 语句操作数据库的方法
- 整理CSS中遇到的一些常见问题
- as3.0实现小涂鸦效果
- ASP.NET 的授权和认证
- CF305C--------数学题
- 面向对象
- 新安装的ubuntu需要下载的东西