Hibernate 一对多外键单向关联
这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1的many-to-one元素增加unique="true"属性即可。?一、模型介绍?一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。?二、实体(省略getter、setter方法)?public class Person1nfk implements Serializable {??? private int personid;??? private String name;??? private int age;??? private Set addresses=new HashSet();?public class Address1nfk implements Serializable {??? private int addressid;??? private String addressdetail;?三、表模型?mysql> desc address_1nfk;+---------------+--------------+------+-----+---------+----------------+| Field???????? | Type???????? | Null | Key | Default | Extra????????? |+---------------+--------------+------+-----+---------+----------------+| addressid???? | int(11)????? | NO?? | PRI | NULL??? | auto_increment || addressdetail | varchar(255) | YES? |???? | NULL??? |??????????????? || personid????? | int(11)?? ???| YES? | MUL | NULL??? |??????????????? |+---------------+--------------+------+-----+---------+----------------+?mysql> desc person_1nfk;+----------+--------------+------+-----+---------+----------------+| 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 10:06 (QP5 v5.50) */? CREATE TABLE `address_1nfk` (??? `addressid` int(11) NOT NULL auto_increment,??? `addressdetail` varchar(255) default NULL,??? `addresses` int(11) default NULL,??? PRIMARY KEY? (`addressid`),??? KEY `FK9B93456DC08D1667` (`addresses`),??? CONSTRAINT `FK9B93456DC08D1667` FOREIGN KEY (`addresses`) REFERENCES `person_1nfk` (`personid`)? ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;?? /* Formatted on 2007/08/21 10:07 (QP5 v5.50) */? CREATE TABLE `person_1nfk` (??? `personid` int(11) NOT NULL auto_increment,??? `name` varchar(255) default NULL,??? `age` int(11) default NULL,??? PRIMARY KEY? (`personid`)? ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;??五、映射方法:在实体类Person里面添加Address的集合,即可形成一对多关系。??????? <!--映射集合属性,关联到持久化类,inverse="false"表示主控端在Person1nfk端,lazy="false"表示不采用延迟加载-->??????? <set name="addresses"???????????? table="ADDRESS_1nfk"???????????? cascade="all"??????? >??????????? <!--确定关联的外键列-->??????????? <key column="personid"/>??????????? <!--用以映射到关联类属性-->??????????? <one-to-many style="padding-bottom: 0cm; padding-left: 5.4pt; width: 728pt; padding-right: 5.4pt; height: 14.25pt; padding-top: 0cm;">??????? </set>?<hibernate-mapping>??? <class name="com.lavasoft.dx._1_n_fk.Person1nfk" table="PERSON_1nfk">??????? <id name="personid">??????????? <generator style="padding-bottom: 0cm; padding-left: 5.4pt; width: 728pt; padding-right: 5.4pt; height: 14.25pt; padding-top: 0cm;">??????? </id>??????? <property name="name"/>??????? <property name="age"/>??????? <!--映射集合属性,关联到持久化类,inverse="false"表示主控端在Person1nfk端,lazy="false"表示不采用延迟加载-->??????? <set name="addresses"???????????? table="ADDRESS_1nfk"???????????? cascade="all"??????? >??????????? <!--确定关联的外键列-->??????????? <key column="personid"/>??????????? <!--用以映射到关联类属性-->??????????? <one-to-many style="padding-bottom: 0cm; padding-left: 5.4pt; width: 728pt; padding-right: 5.4pt; height: 14.25pt; padding-top: 0cm;">??????? </set>??? </class></hibernate-mapping>?<hibernate-mapping>??? <class name="com.lavasoft.dx._1_n_fk.Address1nfk" table="ADDRESS_1nfk">??????? <id name="addressid">??????????? <generator style="padding-bottom: 0cm; padding-left: 5.4pt; width: 728pt; padding-right: 5.4pt; height: 14.25pt; padding-top: 0cm;">??????? </id>??????? <property name="addressdetail"/>??? </class></hibernate-mapping>??六、测试方法?public class Test_1nfk {??? public static void main(String[] args){??????? Address1nfk add1=new Address1nfk();??????? Address1nfk add2=new Address1nfk();??????? Person1nfk p=new Person1nfk();???????? add1.setAddressdetail("郑州市经三路");??????? add2.setAddressdetail("合肥市宿州路");??????? p.setName("wang");??????? p.setAge(30);??????? p.getAddresses().add(add1);??????? p.getAddresses().add(add2);???????? Session session= HibernateUtil.getCurrentSession();??????? Transaction tx=session.beginTransaction();??????? session.save(add1);??????? session.save(add2);??????? session.save(p);??????? tx.commit();??????? HibernateUtil.closeSession();??? }}?七、测试结果?1) :正常保存.//??????? session.save(add1);//??????? session.save(add2);??????? session.save(p);Hibernate: insert into PERSON_1nfk (name, age) values (?Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: update ADDRESS_1nfk set personid=? Hibernate: update ADDRESS_1nfk set personid=? ?2) :正常保存.??????? session.save(add1);??????? session.save(add2);??????? session.save(p);Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into PERSON_1nfk (name, age) values (?Hibernate: update ADDRESS_1nfk set personid=? Hibernate: update ADDRESS_1nfk set personid=? ?3) :正常保存.??????? session.save(add1);??????? session.save(add2);//??????? session.save(p);Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)