Hibernate 一对一外键单向关联

.

Hibernate 一对一外键单向关联
 
    事实上,单向1-1N-1的实质是相同的,1-1N-1的特例,单向1-1N-1的映射配置也非常相似。只需要将原来的many-to-one元素增加unique="true"属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为单向1-1。基于外键的单向1-1的配置将与无连接表N-1关联的many-to-one增加unique="true"属性即可。
 
一、模型介绍
 
一个人(Person)对应一个地址(Address)。
 
二、实体(省略gettersetter方法)
 
public class Person11fk {
    private int personid;
    private String name;
    private int age;
    private Address11fk address11fk;
 
public class Address11fk {
    private int addressid;
    private String addressdetail;
 
三、表模型
 
mysql> desc address_11fk;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
 
mysql> desc person_11fk;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| personid  | int(11)      | NO   | PRI | NULL    | auto_increment |
| name      | varchar(255) | YES  |     | NULL    |                |
| age       | int(11)      | YES  |     | NULL    |                |
| addressId | int(11)      | YES  | UNI | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
 
四、生成的SQL脚本
 
CREATE TABLE `address_11fk` (
    `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 `person_11fk` (
    `personid` int(11) NOT NULL auto_increment,
    `name` varchar(255) default NULL,
    `age` int(11) default NULL,
    `addressId` int(11) default NULL,
    PRIMARY KEY    (`personid`),
    KEY `FK68A8818F3F45AA77` (`addressId`),
    CONSTRAINT `FK68A8818F3F45AA77` FOREIGN KEY (`addressId`) REFERENCES `address_11fk` (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
 
 
五、映射方法:
 
    在Person中添加Address属性,映射配置为:
        <!--用来映射关联PO columnAddress在该表中的外键列名,增加unique变成“1-1-->
        <many-to-one name="address11fk" column="addressId" unique="true"/>
 
<hibernate-mapping>
        <class name="com.lavasoft.dx._1_1_fk.Address11fk" table="ADDRESS_11fk">
                <id name="addressid">
                        <generator class="identity"/>
                </id>
                <property name="addressdetail"/>
        </class>
</hibernate-mapping>
 
<hibernate-mapping>
        <class name="com.lavasoft.dx._1_1_fk.Person11fk" table="PERSON_11fk">
                <id name="personid">
                        <generator class="identity"/>
                </id>
                <property name="name"/>
                <property name="age"/>
                <!--用来映射关联PO column是Address在该表中的外键列名,增加unique变成“1-1”-->
                <many-to-one name="address11fk" column="addressId" unique="true"/>
        </class>
</hibernate-mapping>
       
 
六、测试方法
 
public class Test_11fk {
        public static void main(String[] args){
                Person11fk p1=new Person11fk();
    
                p1.setAge(21);
                p1.setName("p1");
    
                Address11fk add1=new Address11fk();
                add1.setAddressdetail("郑州市经三路");
    
                p1.setAddress11fk(add1);
    
                Session session= HibernateUtil.getCurrentSession();
                Transaction tx=session.beginTransaction();
                session.save(add1);
                session.save(p1);
                tx.commit();
                HibernateUtil.closeSession();
        }
}
 
 
七、测试结果
 
1) :正常保存. 推荐这么干!
        session.save(add1);
        session.save(p1);
 
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
 
2) :正常保存.
        session.save(p1);
        session.save(add1);
 
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: update PERSON_11fk set name=?, age=?, addressId=? where personid=?
 
3) :正常保存.
//        session.save(p1);
        session.save(add1);
 
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
 
4) : 发生异常,不能保存.
        session.save(p1);
//        session.save(add1);
 
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_fk.Address11fk
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值