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

Hibernate 多对一连接表单向关联

2019-11-10 20:26:13
字体:
来源:转载
供稿:网友
Hibernate 多对一连接表单向关联
 
 
一、模型介绍
 
多个人(Person)对应一个地址(Address)。
 
二、实体(省略getter、setter方法)
 
public class Personn1tab {
    PRivate int personid;
    private String name;
    private int age;
    private Addressn1tab addressn1tab;
 
public class Addressn1tab {
    private int addressid;
    private String addressdetail;
 
三、表模型
 
MySQL> desc address_n1tab;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
 
mysql> desc join_n1tab;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| personid   | int(11) | NO   | PRI |         |       |
| addressn1tab | int(11) | YES  | MUL | NULL    |       |
+------------+---------+------+-----+---------+-------+
 
mysql> desc person_n1tab;
+----------+--------------+------+-----+---------+----------------+
| 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脚本
 
CREATE TABLE `address_n1tab` (
  `addressid` int(11) NOT NULL auto_increment,
  `addressdetail` varchar(255) default NULL,
  PRIMARY KEY  (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
 
CREATE TABLE `join_n1tab` (
  `personid` int(11) NOT NULL,
  `address11fk` int(11) default NULL,
  PRIMARY KEY  (`personid`),
  KEY `FKAC780AAADAE3A82C` (`personid`),
  KEY `FKAC780AAAC6242A64` (`address11fk`),
  CONSTRAINT `FKAC780AAAC6242A64` FOREIGN KEY (`address11fk`) REFERENCES `address_n1tab` (`addressid`),
  CONSTRAINT `FKAC780AAADAE3A82C` FOREIGN KEY (`personid`) REFERENCES `person_n1tab` (`personid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 
CREATE TABLE `person_n1tab` (
  `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_1_tab.Personn1tab" table="PERSON_n1tab">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--使用join元素显式确定链接表-->
        <join table="join_n1tab">
            <!--映射关联所用的外键-->
            <key column="personid"/>
            <many-to-one name="addressn1tab"/>
        </join>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_1_tab.Addressn1tab" table="ADDRESS_n1tab">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping>
 
 
六、测试方法
 
public class Test_n1tab {
    public static void main(String[] args){
        Personn1tab p1=new Personn1tab();
        Personn1tab p2=new Personn1tab();
 
        p1.setAge(21);
        p1.setName("p1");
 
        p2.setAge(23);
        p2.setName("p2");
 
        Addressn1tab add=new Addressn1tab();
        add.setAddressdetail("郑州市经三路");
 
        p1.setAddressn1tab(add);
        p2.setAddressn1tab(add);
 
        session session=HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add);
        session.save(p1);
        session.save(p2);
        tx.commit();
        HibernateUtil.closeSession();
    }
}
 
七、测试结果
 
1) :正常保存. 推荐这么干!
 
        session.save(p1);
        session.save(p2);
 
Hibernate: insert into ADDRESS_n1tab (addressdetail) values (?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
 
2) :正常保存.
        session.save(p1);
        session.save(p2);
        session.save(add);
 
 Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
 Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
 Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
 Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
 

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