首页 > 学院 > 开发设计 > 正文

Hibernate 一对多连接表双向关联

2019-11-10 20:21:02
字体:
来源:转载
供稿:网友
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 `FK8F869F61F93DDD6` (`personid`),
  KEY `FK8F869F61FC0F682A` (`addressid`),
  CONSTRAINT `FK8F869F61FC0F682A` FOREIGN KEY (`addressid`) REFERENCES `address_1ntab_sx` (`addressid`),
  CONSTRAINT `FK8F869F61F93DDD6` FOREIGN KEY (`personid`) REFERENCES `person_1ntab_sx` (`personid`)
) 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 (?, ?)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表