Hibernate 一对一主键单向关联

Hibernate 一对一主键单向关联

 

1-1的关联可以基于主键关联,但基于主键关联的持久化类不能拥有自己的主键生成策略,它的主键由关联类负责生成。另外,另外,增加one-to-one元素来关联属性,必须为one-to-one元素增加constrained="true"属性,表明该类主键由关联类生成。

   

一、模型介绍

 

一个人(Person)对应一个地址(Address)。

 

二、实体(省略getter、setter方法)

Person11pk实体类的文件:

public class Person11pk {
    private int personid;
    private String name;
    private int age;
    private Address11pk address11pk;

 Address11pk 实体类:

public class Address11pk {
    private int addressid;
    private String addressdetail;

 三、表模型

 

mysql> desc address_11pk;

+---------------+--------------+------+-----+---------+----------------+

| Field         | Type         | Null | Key | Default | Extra          |

+---------------+--------------+------+-----+---------+----------------+

| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |

| addressdetail | varchar(255) | YES  |     | NULL    |                |

+---------------+--------------+------+-----+---------+----------------+

 

mysql> desc person_11pk;

+----------+--------------+------+-----+---------+-------+

| Field    | Type         | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| personid | int(11)      | NO   | PRI |         |       |

| name     | varchar(255) | YES  |     | NULL    |       |

| age      | int(11)      | YES  |     | NULL    |       |

+----------+--------------+------+-----+---------+-------+

 

四、生成的SQL脚本

/* Formatted on 2007/08/22 14:41 (QP5 v5.50) */
CREATE TABLE `person_11pk` (
  `presonid` int(11) NOT NULL,
  `name` varchar(255) default NULL,
  `age` int(11) default NULL,
  PRIMARY KEY  (`presonid`),
  KEY `FK68A882C591BB393E` (`presonid`),
  CONSTRAINT `FK68A882C591BB393E` FOREIGN KEY (`presonid`) REFERENCES `address_11pk` (`addressid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 五、映射方法:在Person中配置id生成策略为:
 <id name="personid">
            <!--基于主键关联时,主键生成策略是foreign,表明根据关联类生成主键-->
            <generator class="foreign">
                <!--关联持久化类的属性名-->
                <param name="property">address11pk</param>
            </generator>
        </id>
        ......
<!--用于映射1-1关联-->
<one-to-one name="address11pk" constrained="true"/>
 即是如下的具体信息:

 

 

<hibernate-mapping>
    <class name="com.lavasoft.dx._1_1_pk.Person11pk" table="PERSON_11pk">
        <id name="personid" column="presonid">
            <!--基于主键关联时,主键生成策略是foreign,表明根据关联类生成主键-->
            <generator class="foreign">
                <!--关联持久化类的属性名-->
                <param name="property">address11pk</param>
            </generator>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--用于映射1-1关联-->
        <one-to-one name="address11pk" constrained="true"/>
    </class>
</hibernate-mapping>
 Address11pk实体类对应的映射文件为:

 

 

<hibernate-mapping>
    <class name="com.lavasoft.dx._1_1_pk.Address11pk" table="ADDRESS_11pk">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping>
  六、测试方法
public class Test_11pk {
    public static void main(String[] args){
        Person11pk p1=new Person11pk();
 
        p1.setAge(21);
        p1.setName("p1");
 
        Address11pk add1=new Address11pk();
        add1.setAddressdetail("郑州市经三路");
 
        p1.setAddress11pk(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.Address11f
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值