- 浏览: 198777 次
- 性别:
- 来自: 合肥
文章分类
- 全部博客 (156)
- 职场人生 (6)
- 开发时遇见的问题 (14)
- 技术学习 (13)
- Hibernate开发与实战学习 (10)
- Junit (9)
- 设计模式 (4)
- Lucene (1)
- Log4J (1)
- JBPM (0)
- OA (0)
- Spring (0)
- Struts2 (1)
- Android (2)
- Python (1)
- 生活点点滴滴 (8)
- Java (11)
- JavaScript&Jquery (22)
- Hadoop (1)
- JSP&Servlet (8)
- dom4j (2)
- mysql (2)
- Oracle (7)
- PHP (0)
- Jmeter (1)
- 测试方面的东东 (1)
- 励志文章 (3)
- CSS (4)
- XHTML&HTML (1)
- Linux相关 (7)
- C&C++ (2)
- Unix C (3)
- 开发软件配置及软件应用 (2)
- English Study And Movie Scripts (3)
- My English World (1)
最新评论
-
yingzhixing:
不错不错~~
利用org.springframework.web.util中的HtmlUtils工具类 -
yonguo:
博主能把完整的pom配置贴一下共享吗?
在maven中使用YUI-Compressor -
yan578351314:
我的为什么会出现语法错误呢?一大推错误信息...
在maven中使用YUI-Compressor -
jstenjoy:
什么是JBPM -
疯狂的草儿:
cat_rat 写道我是2010年9月份进入公司的--2010 ...
辞职了,离开了我工作的第一家公司
第九章Hibernate映射一对一关联关系
- 共享主键关联
- 唯一外键关联
1.两个关联表使用相同的主键值
数据准备:
-- Create table create table USERS1 ( USERNAME VARCHAR2(40), PASSWORD VARCHAR2(40), ID NUMBER(8) not null )
Profile表用于保存用户的其他信息。
-- Create table create table PROFILE ( ID NUMBER(8) not null, EMAIL VARCHAR2(200), PHONE VARCHAR2(20), MOBILE VARCHAR2(20), ADDRESS VARCHAR2(200), POSTCODE VARCHAR2(10) )
其中,ID即是主键又是外键。因此称为共享主键。
User:
public class User implements java.io.Serializable { private static final long serialVersionUID = 1L; private Integer id; private String username; private String password; private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } public User() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
User映射配置:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.User" table="USERS1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment"></generator> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="40" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="40" /> </property> <one-to-one name="profile" class="com.crazy.Profile"></one-to-one> </class> </hibernate-mapping>
Profile类:
public class Profile implements java.io.Serializable { private Integer id; private String email; private String phone; private String mobile; private String address; private String postcode; private User user; public Profile() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return this.postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } }
Profile映射配置:
其主键是使用user的。
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Profile" table="PROFILE" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="foreign"> <param name="property">user</param> </generator> </id> <property name="email" type="java.lang.String"> <column name="EMAIL" length="200" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> <property name="mobile" type="java.lang.String"> <column name="MOBILE" length="20" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="200" /> </property> <property name="postcode" type="java.lang.String"> <column name="POSTCODE" length="10" /> </property> <one-to-one name="user" class="com.crazy.User"></one-to-one> </class> </hibernate-mapping>
测试代码:
public class HibernateTest { public static void main(String args[]){ HibernateTest test = new HibernateTest(); test.add(); } public void add(){ Profile profile = new Profile(); profile.setAddress("河南胜利路"); profile.setEmail("yesmeshtu2008@163.com"); profile.setMobile("13301239472145"); User user = new User(); profile.setUser(user); user.setUsername("grass"); user.setPassword("123"); user.setProfile(profile); Session session = new Configuration().configure().buildSessionFactory().openSession(); session.beginTransaction(); session.save(profile); session.save(user); session.getTransaction().commit(); } }
2.多对一关联的特殊形式,要求多方唯一
User类:
public class User implements java.io.Serializable { private Integer id; private String username; private String password; private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } public User() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
User映射配置:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.User" table="USERS1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="40" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="40" /> </property> <one-to-one name="profile" class="com.crazy.Profile"></one-to-one> </class> </hibernate-mapping>
Profile类:
public class Profile implements java.io.Serializable { private Integer id; private String email; private String phone; private String mobile; private String address; private String postcode; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Profile() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return this.postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } }
Profile映射配置:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Profile" table="PROFILE1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="email" type="java.lang.String"> <column name="EMAIL" length="200" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> <property name="mobile" type="java.lang.String"> <column name="MOBILE" length="20" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="200" /> </property> <property name="postcode" type="java.lang.String"> <column name="POSTCODE" length="10" /> </property> <many-to-one name="user" class="com.crazy.User" unique="true"> <column name="user_id"></column> </many-to-one> </class> </hibernate-mapping>
配置如下:
<many-to-one name="user" class="com.crazy.User" unique="true"> <column name="user_id"></column> </many-to-one>
测试代码:
public class HibernateTest { public static void main(String args[]){ HibernateTest test = new HibernateTest(); test.add(); } public void add(){ Profile profile = new Profile(); profile.setAddress("河南胜利路"); profile.setEmail("yesmeshtu2008@163.com"); profile.setMobile("13309472145"); User user = new User(); profile.setUser(user); user.setUsername("grass"); user.setPassword("123"); user.setProfile(profile); Session session = new Configuration().configure().buildSessionFactory().openSession(); session.beginTransaction(); session.save(profile); session.save(user); session.getTransaction().commit(); } }
发表评论
-
第八章Hibernate映射多对一关联关系
2011-01-26 08:51 940第八章Hibernate映射多对一关联关系 多对 ... -
第七章Hibernate 组件(component)映射
2011-01-25 17:05 915第七章 Hibernate组件(component)映射 ... -
第六章Hibernate-的集合映射
2011-01-22 09:44 1061第六章Hibernate-的集合映射 Set映射 ... -
第五章Hibernate-OSIV与泛型DAO模式
2011-01-21 14:02 1247第五章Hibernate -OSIV与泛型DAO模式 ... -
第四章Hibernate持久化对象的生命周期
2011-01-21 10:59 751第四章 持久化对象的生命周期 持久化对象生命周期 ... -
第三章标识符属性生成策略与对象识别
2011-01-19 21:56 927第三章标识符属性生成 ... -
第二章Hibernate的核心接口与类
2011-01-14 15:51 876Hibernate中的核心接口与类 Con ... -
hibernate经典问题的经典贴
2009-12-24 09:26 795Starting 我们为什么要学习Hibernate ... -
第一章Hibernate开发与实战学习
2009-12-20 15:29 1210首先要做一下准备工作,即获得所有的jar包和相应的工具。 1 ...
相关推荐
hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person---->IdCard) 8 hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射...
15.1 映射一对一关联 15.1.1 按照外键映射 15.1.2 按照主键映射 15.2 映射单向多对多关联 15.3 映射双向多对多关联关系 15.3.1 关联两端使用元素 15.3.2 在inverse端使用元素 15.3.3 使用组件类...
15.1 映射一对一关联 15.1.1 按照外键映射 15.1.2 按照主键映射 15.2 映射单向多对多关联 15.3 映射双向多对多关联关系 15.3.1 关联两端使用元素 15.3.2 在inverse端使用元素 15.3.3 使用组件类...
15.1 映射一对一关联 15.1.1 按照外键映射 15.1.2 按照主键映射 15.2 映射单向多对多关联 15.3 映射双向多对多关联关系 15.3.1 关联两端使用元素 15.3.2 在inverse端使用元素 15.3.3 使用组件类...
15.1 映射一对一关联 15.1.1 按照外键映射 15.1.2 按照主键映射 15.2 映射单向多对多关联 15.3 映射双向多对多关联关系 15.3.1 关联两端使用元素 15.3.2 在inverse端使用元素 15.3.3 使用组件类...
7.3.1 多态的多对一关联 7.3.2 多态集合 7.3.3 对联合的多态关联 7.3.4 每个具体类一张多态表 7.4 小结 第8章 遗留数据库和定制SQL 8.1 整合遗留数据库 8.1.1 处理主键 8.1.2 带...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional ...
8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...
7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional ...
12.4.4 一对一关联的annotation注解实现536 12.4.5 多对一单向关联的annotation注解实现538 12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向...
一对一 5.1.12. 组件(component), 动态组件(dynamic-component) 5.1.13. 子类(subclass) 5.1.14. 连接的子类(joined-subclass) 5.1.15. map, set, list, bag 5.1.16. 引用(import) 5.2. ...
8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...
8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...