1、Hibernate 一对多外键单向关联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 i
2、nt 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_incremen
3、t | 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 | | a
4、ge | 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
5、),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) de
6、fault NULL,PRIMARY KEY (personid) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;五、映射方法:在实体类 Person 里面添加 Address 的集合,即可形成一对多关系。六、测试方法public class Test_1nfk public static void main(String args)Address1nfk add1=new Address1nfk();Address1nfk add2=new Address1nfk();Person1nfk p=new Person1nfk();add1
7、.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);mit();Hi
8、bernateUtil.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: updat
9、e ADDRESS_1nfk set personid=? where addressid=?Hibernate: update ADDRESS_1nfk set personid=? where addressid=?2) :正常保存.session.save(add1);session.save(add2);session.save(p);Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hib
10、ernate: insert into PERSON_1nfk (name, age) values (?, ?)Hibernate: update ADDRESS_1nfk set personid=? where addressid=?Hibernate: update ADDRESS_1nfk set personid=? where addressid=?3) :正常保存.session.save(add1);session.save(add2);/ session.save(p);Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)