Hibernate 多对一连接表单向关联

一、模型介绍
 
多个人(Person)对应一个地址(Address)。
 
二、实体(省略gettersetter方法)
 
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 (?, ?)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值