一、模型介绍 |
一个人可对应多个地址,一个地址也可以对应多个人。 |
二、实体(省略getter、setter方法) |
public class Personnn { |
PRivate int personid; |
private String name; |
private int age; |
private Set addresses=new HashSet(); |
public class Addressnn { |
private int addressid; |
private String addressdetail; |
三、表模型 |
MySQL> desc person_nn; |
+----------+--------------+------+-----+---------+----------------+ |
| 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 join_nn; |
+-----------+---------+------+-----+---------+-------+ |
| Field | Type | Null | Key | Default | Extra | |
+-----------+---------+------+-----+---------+-------+ |
| personid | int(11) | NO | PRI | | | |
| addressid | int(11) | NO | PRI | | | |
+-----------+---------+------+-----+---------+-------+ |
mysql> desc person_nn; |
+----------+--------------+------+-----+---------+----------------+ |
| Field | Type | Null | Key | Default | Extra | |
+----------+--------------+------+-----+---------+----------------+ |
| personid | int(11) | NO | PRI | NULL | auto_increment | |
| name | varchar(255) | YES | | NULL | | |
| age | int(11) | YES | | NULL | | |
+----------+--------------+------+-----+---------+----------------+ |
四、生成的SQL脚本 |
/* Formatted on 2007/08/21 11:13 (QP5 v5.50) */ |
CREATE TABLE `address_nn` ( |
`addressid` int(11) NOT NULL auto_increment, |
`addressdetail` varchar(255) default NULL, |
PRIMARY KEY (`addressid`) |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk; |
/* Formatted on 2007/08/21 11:14 (QP5 v5.50) */ |
CREATE TABLE `join_nn` ( |
`personid` int(11) NOT NULL, |
`addressid` int(11) NOT NULL, |
PRIMARY KEY (`personid`,`addressid`), |
KEY `FKAAB98CF5E008E752` (`personid`), |
KEY `FKAAB98CF5239F6A16` (`addressid`), |
CONSTRAINT `FKAAB98CF5239F6A16` FOREIGN KEY (`addressid`) REFERENCES `address_nn` (`addressid`), |
CONSTRAINT `FKAAB98CF5E008E752` FOREIGN KEY (`personid`) REFERENCES `person_nn` (`personid`) |
) ENGINE=InnoDB DEFAULT CHARSET=gbk; |
/* Formatted on 2007/08/21 11:14 (QP5 v5.50) */ |
CREATE TABLE `person_nn` ( |
`personid` int(11) NOT NULL auto_increment, |
`name` varchar(255) default NULL, |
`age` int(11) default NULL, |
PRIMARY KEY (`personid`) |
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk; |
五、映射方法 |
<hibernate-mapping> |
<class name="com.lavasoft.dx._n_n.Personnn" table="PERSON_nn"> |
<id name="personid"> |
<generator class="identity"/> |
</id> |
<property name="name"/> |
<property name="age"/> |
<!--映射集合属性,join_1ntab是连接表表名--> |
<set name="addresses" |
table="join_nn" |
> |
<!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名--> |
<key column="personid"/> |
<!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名--> |
<many-to-many |
column="addressid" |
class="com.lavasoft.dx._n_n.Addressnn"/> |
</set> |
</class> |
</hibernate-mapping> |
<hibernate-mapping> |
<class name="com.lavasoft.dx._n_n.Addressnn" table="ADDRESS_nn"> |
<id name="addressid"> |
<generator class="identity"/> |
</id> |
<property name="addressdetail"/> |
</class> |
</hibernate-mapping> |
六、测试方法 |
public class Test_nn { |
public static void main(String[] args){ |
Addressnn add1=new Addressnn(); |
Addressnn add2=new Addressnn(); |
Addressnn add3=new Addressnn(); |
Personnn p1=new Personnn(); |
Personnn p2=new Personnn(); |
add1.setAddressdetail("郑州市经三路"); |
add2.setAddressdetail("合肥市宿州路"); |
add3.setAddressdetail("北京市长安路"); |
p1.setName("wang"); |
p1.setAge(30); |
p2.setName("lee"); |
p2.setAge(50); |
p1.getAddresses().add(add1); |
p1.getAddresses().add(add2); |
p2.getAddresses().add(add2); |
p2.getAddresses().add(add3); |
session session= HibernateUtil.getCurrentSession(); |
Transaction tx=session.beginTransaction(); |
session.save(add1); |
session.save(add2); |
session.save(add3); |
session.save(p1); |
session.save(p2); |
tx.commit(); |
HibernateUtil.closeSession(); |
} |
} |
七、测试结果 |
1) :正常保存. |
session.save(add1); |
session.save(add2); |
session.save(add3); |
session.save(p1); |
session.save(p2); |
Hibernate: insert into ADDRESS_nn (addressdetail) values (?) |
Hibernate: insert into ADDRESS_nn (addressdetail) values (?) |
Hibernate: insert into ADDRESS_nn (addressdetail) values (?) |
Hibernate: insert into PERSON_nn (name, age) values (?, ?) |
Hibernate: insert into PERSON_nn (name, age) values (?, ?) |
Hibernate: insert into join_nn (personid, addressid) values (?, ?) |
Hibernate: insert into join_nn (personid, addressid) values (?, ?) |
Hibernate: insert into join_nn (personid, addressid) values (?, ?) |
Hibernate: insert into join_nn (personid, addressid) values (?, ?) |
新闻热点
疑难解答