Hibernate 一对多连接表双向关联 |
|
一、模型介绍 |
|
一个人(Person)对应多个地址(Address)。 |
|
二、实体(省略getter、setter方法) |
|
public class Person1ntab_sx { |
private int personid; |
private String name; |
private int age; |
private Set addresses=new HashSet(); |
|
public class Address1ntab_sx { |
private int addressid; |
private String addressdetail; |
private Person1ntab_sx person1ntab_sx; |
|
三、表模型 |
|
mysql> desc person_1ntab_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_1ntab_sx; |
+---------------+--------------+------+-----+---------+----------------+ |
| Field | Type | Null | Key | Default | Extra | |
+---------------+--------------+------+-----+---------+----------------+ |
| addressid | int(11) | NO | PRI | NULL | auto_increment | |
| addressdetail | varchar(255) | YES | | NULL | | |
+---------------+--------------+------+-----+---------+----------------+ |
|
mysql> desc join_1ntab_sx; |
+-----------+---------+------+-----+---------+-------+ |
| Field | Type | Null | Key | Default | Extra | |
+-----------+---------+------+-----+---------+-------+ |
| addressid | int(11) | NO | PRI | | | |
| personid | int(11) | NO | PRI | | | |
+-----------+---------+------+-----+---------+-------+ |
|
四、生成的SQL脚本 |
|
/* Formatted on 2007/08/22 17:52 (QP5 v5.50) */ |
CREATE TABLE `address_1ntab_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 17:52 (QP5 v5.50) */ |
CREATE TABLE `person_1ntab_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:52 (QP5 v5.50) */ |
CREATE TABLE `join_1ntab_sx` ( |
`addressid` int(11) NOT NULL, |
`personid` int(11) NOT NULL, |
PRIMARY KEY (`personid`,`addressid`), |
KEY `FK |
KEY `FK |
CONSTRAINT `FK |
CONSTRAINT `FK |
) ENGINE=InnoDB DEFAULT CHARSET=gbk; |
|
五、映射方法 |
|
<hibernate-mapping> |
<class name="com.lavasoft.sx._1_n_tab.Person1ntab_sx" table="PERSON_1ntab_sx"> |
<id name="personid"> |
<generator class="identity"/> |
</id> |
<property name="name"/> |
<property name="age"/> |
<!--映射集合属性,关联到持久化类--> |
<!--table="join_1ntab_sx"指定了连接表的名字--> |
<set name="addresses" |
table="join_1ntab_sx" |
cascade="all"> |
<!--column="personid"指定连接表中关联当前实体类的列名--> |
<key column="personid" not-null="true"/> |
<!--unique="true"表示当前实体类是"1",不是"n"--> |
<many-to-many column="addressid" |
unique="true" |
class="com.lavasoft.sx._1_n_tab.Address1ntab_sx"/> |
</set> |
</class> |
</hibernate-mapping> |
|
<hibernate-mapping> |
<class name="com.lavasoft.sx._1_n_tab.Address1ntab_sx" |
table="ADDRESS_1ntab_sx"> |
<id name="addressid"> |
<generator class="identity"/> |
</id> |
<property name="addressdetail"/> |
<!--映射关联属性,column属性指定外键列名--> |
<join table="join_1ntab_sx" |
inverse="true" |
optional="true"> |
<key column="addressid"/> |
<many-to-one name="person1ntab_sx" |
column="personid" |
cascade="all" |
not-null="true"/> |
</join> |
</class> |
</hibernate-mapping> |
|
六、测试方法 |
|
public class Test_1ntab_sx { |
public static void main(String[] args){ |
Address1ntab_sx add1=new Address1ntab_sx(); |
Address1ntab_sx add2=new Address1ntab_sx(); |
Person1ntab_sx p=new Person1ntab_sx(); |
|
add1.setAddressdetail("郑州市经三路"); |
add2.setAddressdetail("合肥市宿州路"); |
p.setName("wang"); |
p.setAge(30); |
|
p.getAddresses().add(add1); |
p.getAddresses().add(add2); |
add1.setPerson1ntab_sx(p); |
add2.setPerson1ntab_sx(p); |
|
Session session= HibernateUtil.getCurrentSession(); |
Transaction tx=session.beginTransaction(); |
// session.save(p); |
session.saveOrUpdate(add1); |
session.saveOrUpdate(add2); |
tx.commit(); |
HibernateUtil.closeSession(); |
} |
} |
|
七、测试结果 |
|
1) :正常保存. |
// session.save(p); |
session.saveOrUpdate(add1); |
session.saveOrUpdate(add2); |
|
Hibernate: insert into PERSON_1ntab_sx (name, age) values (?, ?) |
Hibernate: insert into ADDRESS_1ntab_sx (addressdetail) values (?) |
Hibernate: insert into ADDRESS_1ntab_sx (addressdetail) values (?) |
Hibernate: insert into join_1ntab_sx (personid, addressid) values (?, ?) |
Hibernate: insert into join_1ntab_sx (personid, addressid) values (?, ?) |
Hibernate 一对多连接表双向关联
原创
©著作权归作者所有:来自51CTO博客作者leizhimin的原创作品,如需转载,请与作者联系,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
websockt一对一聊天java部分
websocket
java websocket -
Hibernate5-一对多双向关联-左外连接-HQL
Hibernate5-一对多双向关联-左外连接-HQL
双向关联 左外连接 Hibernate5 -
hibernate关联映射 一对多
一对多的映射: 在之前配置中多的一方保持之前不变 在一的一方需要配置:  
hibernate 一对多 关联映射