- 浏览: 35027 次
- 性别:
文章分类
最新评论
-
开源人:
...
简约之美,JQuery之进度条插件 -
zhubo123:
楼主. 有个问题想请教你.我试着做了一遍. 可是为什么setP ...
JAVA文件上传(带进度条)
一、模型介绍
|
|||||||
|
|||||||
一个人(Person)对应一个地址(Address)。
|
|||||||
|
|||||||
二、实体(省略getter、setter方法)
|
|||||||
|
|||||||
public class Person11tab_sx {
|
|||||||
private int personid;
|
|||||||
private String name;
|
|||||||
private int age;
|
|||||||
private Address11tab_sx address11tab_sx;
|
|||||||
|
|||||||
public class Address11tab_sx {
|
|||||||
private int addressid;
|
|||||||
private String addressdetail;
|
|||||||
private Person11tab_sx person11tab_sx;
|
|||||||
|
|||||||
三、表模型
|
|||||||
|
|||||||
mysql> desc person_11tab_sx;
|
|||||||
+----------+--------------+------+-----+---------+----------------+
|
|||||||
| Field | Type | Null | Key | Default | Extra |
|
|||||||
+----------+--------------+------+-----+---------+----------------+
|
|||||||
| personid | int(11) | NO | PRI | NULL | auto_increment |
|
|||||||
| name | varchar(255) | YES | | NULL | |
|
|||||||
| age | int(11) | YES | | NULL | |
|
|||||||
+----------+--------------+------+-----+---------+----------------+
|
|||||||
|
|||||||
mysql> desc address_11tab_sx;
|
|||||||
+---------------+--------------+------+-----+---------+----------------+
|
|||||||
| Field | Type | Null | Key | Default | Extra |
|
|||||||
+---------------+--------------+------+-----+---------+----------------+
|
|||||||
| addressid | int(11) | NO | PRI | NULL | auto_increment |
|
|||||||
| addressdetail | varchar(255) | YES | | NULL | |
|
|||||||
+---------------+--------------+------+-----+---------+----------------+
|
|||||||
|
|||||||
四、生成的SQL脚本
|
|||||||
|
|||||||
/* Formatted on 2007/08/22 17:35 (QP5 v5.50) */
|
|||||||
CREATE TABLE `person_11tab_sx` (
|
|||||||
`personid` int(11) NOT NULL auto_increment,
|
|||||||
`name` varchar(255) default NULL,
|
|||||||
`age` int(11) default NULL,
|
|||||||
PRIMARY KEY (`personid`)
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
|
|||||||
|
|||||||
/* Formatted on 2007/08/22 17:34 (QP5 v5.50) */
|
|||||||
CREATE TABLE `address_11tab_sx` (
|
|||||||
`addressid` int(11) NOT NULL auto_increment,
|
|||||||
`addressdetail` varchar(255) default NULL,
|
|||||||
PRIMARY KEY (`addressid`)
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
/* Formatted on 2007/08/22 18:35 (QP5 v5.50) */
CREATE TABLE `join_11tab_sx` ( `addressid` int(11) NOT NULL, `personid` int(11) NOT NULL, PRIMARY KEY (`personid`), UNIQUE KEY `addressid` (`addressid`), UNIQUE KEY `personid` (`personid`), KEY `FKF4AA80E44327AAB6` (`personid`), KEY `FKF4AA80E460C0C9F0` (`addressid`), CONSTRAINT `FKF4AA80E460C0C9F0` FOREIGN KEY (`addressid`) REFERENCES `address_11tab_sx` (`addressid`), CONSTRAINT `FKF4AA80E44327AAB6` FOREIGN KEY (`personid`) REFERENCES `person_11tab_sx` (`personid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; |
|||||||
|
|||||||
|
|||||||
五、映射方法
|
|||||||
|
|||||||
<hibernate-mapping>
|
|||||||
<class name="com.lavasoft.sx._1_1_tab.Person11tab_sx" table="PERSON_11tab_sx">
|
|||||||
<id name="personid">
|
|||||||
<generator class="identity"/>
|
|||||||
</id>
|
|||||||
<property name="name"/>
|
|||||||
<property name="age"/>
|
|||||||
<join table="join_11tab_sx"
|
|||||||
optional="true">
|
|||||||
<key column="personid"
|
|||||||
unique="true"/>
|
|||||||
<many-to-one name="address11tab_sx"
|
|||||||
column="addressid"
|
|||||||
not-null="true"
|
|||||||
unique="true"/>
|
|||||||
</join>
|
|||||||
</class>
|
|||||||
</hibernate-mapping>
|
|||||||
|
|||||||
<hibernate-mapping>
|
|||||||
<class name="com.lavasoft.sx._1_1_tab.Address11tab_sx" table="ADDRESS_11tab_sx">
|
|||||||
<id name="addressid">
|
|||||||
<generator class="identity"/>
|
|||||||
</id>
|
|||||||
<property name="addressdetail"/>
|
|||||||
<join table="join_11tab_sx"
|
|||||||
optional="true"
|
|||||||
inverse="true">
|
|||||||
<key column="addressid"
|
|||||||
unique="true"/>
|
|||||||
<many-to-one name="person11tab_sx" column="personid"
|
|||||||
not-null="true" unique="true"/>
|
|||||||
</join>
|
|||||||
</class>
|
|||||||
</hibernate-mapping>
|
|||||||
|
|||||||
六、测试方法
|
|||||||
|
|||||||
public class Test_11tab_sx {
|
|||||||
public static void main(String[] args){
|
|||||||
Address11tab_sx add = new Address11tab_sx();
|
|||||||
Person11tab_sx p = new Person11tab_sx();
|
|||||||
|
|||||||
add.setAddressdetail("郑州市经三路");
|
|||||||
p.setAge(12);
|
|||||||
p.setName("wudalang");
|
|||||||
|
|||||||
add.setPerson11tab_sx(p);
|
|||||||
p.setAddress11tab_sx(add);
|
|||||||
|
|||||||
Session session = HibernateUtil.getCurrentSession();
|
|||||||
Transaction tx = session.beginTransaction();
|
|||||||
session.saveOrUpdate(p);
|
|||||||
session.saveOrUpdate(add);
|
|||||||
tx.commit();
|
|||||||
HibernateUtil.closeSession();
|
|||||||
}
|
|||||||
}
|
|||||||
|
|||||||
七、测试结果
|
|||||||
|
|||||||
1) :正常保存.
|
|||||||
session.saveOrUpdate(p);
|
|||||||
session.saveOrUpdate(add);
|
|||||||
|
|||||||
Hibernate: insert into PERSON_11tab_sx (name, age) values (?, ?)
|
|||||||
Hibernate: insert into ADDRESS_11tab_sx (addressdetail) values (?)
|
|||||||
Hibernate: insert into join_11tab_sx (addressid, personid) values (?, ?)
|
发表评论
-
Hibernate 多对多双向关联
2012-01-30 16:06 619一、模型介绍 多个人( ... -
Hibernate 一对多连接表双向关联
2012-01-30 16:06 545一、模型介绍 一个人( ... -
Hibernate 一对多外键双向关联
2012-01-30 16:08 602一、模型介绍 一个人( ... -
Hibernate 一对一主键双向关联
2012-02-09 11:09 725一对一主键映射在一对一映射中还算是最为常用的。 一、模 ... -
Hibernate 一对一外键双向关联
2012-01-07 09:26 591一对一外键关联是一对多外键关联的特例,只是在多的一方加了 ... -
Hibernate 多对多单向关联
2012-01-07 08:52 601一、模型介绍 多个人(P ... -
Hibernate 多对一连接表单向关联
2012-01-07 08:52 648一、模型介绍 多个人( ... -
Hibernate 多对一外键单向关联
2012-01-07 08:52 586一、模型介绍 多个人( ... -
Hibernate 一对多连接表单向关联
2012-01-07 08:52 707一、模型介绍 一个人( ... -
Hibernate 一对多外键单向关联
2012-01-06 15:16 703这种情况很少见, ... -
Hibernate 一对一连接表单向关联
2012-01-06 15:14 606这种情况很少见,但Hibernate同样允许采用 ... -
Hibernate 一对一主键单向关联
2012-01-06 15:12 6081-1的关联可以基于主键关联,但基于主键关联的持久化 ... -
Hibernate 一对一外键单向关联
2012-01-06 15:08 609事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特 ...
相关推荐
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向...Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 ...├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
NULL 博文链接:https://dreamzhong.iteye.com/blog/1201437
hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session ...
一对一关联可能是双向的,在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向主体的关联属性。例子...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...
Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...
8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...
使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...
8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...
7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3 映射一对多双向自身关联关系 7.4 改进持久化类 7.5 小结 7.6 思考题 第8章 通过Hibernate操纵对象(上) ...
8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...
使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件...