`
talentkep
  • 浏览: 97923 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

one-to-one 一对一主键关联映射_单向

    博客分类:
 
阅读更多
one-to-one 一对一主键关联映射_单向

一对一主键关联映射——关联的两个实体共享一个主键(让两个实体对象的id保持相同),这样可以避免多余的字段被创建 .

(单向关联Person---->IdCard)


类:

public class IdCard {

private Integer id;
private String idNo;

}


public class Person {

private Integer id;
private String name;
private IdCard idCard;

}


hbm.xml

IdCard.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.model">
    <class name="IdCard" table="idcard1" >
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native" />
        </id>
        <property name="idNo" column="id_no" type="java.lang.String" />
    </class>

</hibernate-mapping>


Person.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zd.model">
    <class name="Person" table="person1" >
        <id name="id" column="id" type="java.lang.Integer">

<!-- person的主键来源idCard,也就是共享idCard的主键 -->
            <generator class="foreign">
                <param name="property">idCard</param>
            </generator>
        </id>
        <property name="name" column="name" length="50" type="java.lang.String" />

<one-to-one name="idCard" ></one-to-one>
<!--one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,

one-to-one的属性cascade=all ,即新增Person时,同时可新增idcard,

constrained="true", 表明当前主键上存在一个约束,person的主键作为外键参照了idCard 
        <one-to-one name="idCard" constrained="true"></one-to-one>

-->
    </class>

</hibernate-mapping>




运用例子:

public void testSave1(){
   Session session = null;
   Transaction ta = null;
   try{
    session = HibernateUtil.getSession();
    ta = session.beginTransaction();
    IdCard idCard = new IdCard();
    idCard.setIdNo("88888888888888");
    Person p = new Person();
    p.setName("Tom");
    p.setIdCard(idCard);

//session.save(idCard); //可删除,因one-to-one 默认casedese=all
    session.save(p); // 保存idCard, person2个。
    ta.commit();
   }catch(Exception e){
    e.printStackTrace();
    if(ta != null){
     ta.rollback();
    }
   }finally{
    //关闭session, user变为detached离线对象
    HibernateUtil.closeSession(session);
   }
 
}



Hibernate: insert into idcard1 (id_no) values (?)
Hibernate: insert into person1 (name, id) values (?, ?)



public void testGet1(){
   Session session = null;
   Transaction ta = null;
   try{
    session = HibernateUtil.getSession();
    ta = session.beginTransaction();
    Person p = (Person) session.get(Person.class, new Integer(2));
   System.out.println("person.name=" + p.getName());
    System.out.println("idCard.cardNo=" + p.getIdCard().getIdNo());
    ta.commit();
   }catch(Exception e){
    e.printStackTrace();
    if(ta != null){
     ta.rollback();
    }
   }finally{
    //关闭session, user变为detached离线对象
    HibernateUtil.closeSession(session);
   }
 
}


Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_ from person1 person0_ where person0_.id=?
person.name=Tom
Hibernate: select idcard0_.id as id0_0_, idcard0_.id_no as id2_0_0_ from idcard1 idcard0_ where idcard0_.id=?
idCard.cardNo=88888888888888



===================================================================

戓要改为双向,修改以下:

public class IdCard {

private Integer id;
private String idNo;
private Person person; //加个对象变量



IdCard.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zd.model">
    <class name="IdCard" table="idcard1" >
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native" />
        </id>
        <property name="idNo" column="id_no" type="java.lang.String" />
        <one-to-one name="person"></one-to-one>
    </class>

</hibernate-mapping>


测试用例:

public void testGet2(){
   Session session = null;
   Transaction ta = null;
   try{
    session = HibernateUtil.getSession();
    ta = session.beginTransaction();
   IdCard ic = (IdCard)session.get(IdCard.class, new Integer(1));
    System.out.println("IdCard.idNo =" + ic.getIdNo());
    System.out.println("Person.name = " + ic.getPerson().getName());
    ta.commit();
   }catch(Exception e){
    e.printStackTrace();
    if(ta != null){
     ta.rollback();
    }
   }finally{
    //关闭session, user变为detached离线对象
    HibernateUtil.closeSession(session);
   }
 
}

执行一条sql语句,且是join, 因为one-to-one默认是fetch="join"

Hibernate: select idcard0_.id as id0_1_, idcard0_.id_no as id2_0_1_, person1_.id as id1_0_, person1_.name as name1_0_ from idcard1 idcard0_ left outer join person1 person1_ on idcard0_.id=person1_.id where idcard0_.id=?
IdCard.idNo =88888888888888
Person.name = Tom

若IdCard.hbm.xml 中<one-to-one name="person" fetch="select"></one-to-one> ,执行2条sql

Hibernate: select idcard0_.id as id0_0_, idcard0_.id_no as id2_0_0_ from idcard1 idcard0_ where idcard0_.id=?
Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_ from person1 person0_ where person0_.id=?
IdCard.idNo =88888888888888
Person.name = Tom



================================

fetch取值"select",表示序列选择抓取,还有一个值是"join",表示采用外连接抓取。

分享到:
评论

相关推荐

    Hibernate学习笔记

    009 一对一 主键关联映射_单向 010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多...

    hibernate学习笔记

    hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person----&gt;IdCard) 10 hibernate一对一...

    Hibernate_Annotation关联映射

    1.共享主键的一对一关联映射: @Entity @Table(name="Test_Body") public class Body { private Integer id; private Heart heart; @Id public Integer getId() { return id; } public void setId(Integer id...

    unidirectional-one2one-sharedprimarykey:该存储库正在存储有关Hibernate映射示例的源代码-共享主键上的单向一对一关联-one source code

    单向一对一共享主键 该存储库正在存储有关Hibernate映射示例的源代码-共享主键上的单向一对一关联

    unidirectional-one2one-foreignkey:该存储库正在存储有关Hibernate映射示例的源代码-一对一-one source code

    Hibernate映射-单向关联 外键一对一关联 在此示例中,我创建两个表:人员和地址。 这两个表都有一个自动递增的id作为主键。 人员表持有地址表中的外键。

    Hibernate3的帮助文档

    8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ...

    hibernate 框架详解

    一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    hibernate 体系结构与配置 参考文档(html)

    一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...

    Hibernate 中文 html 帮助文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    hibernate3.04中文文档.chm

    8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    Hibernate教程

    8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...

    最全Hibernate 参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate3+中文参考文档

    7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的...

    Hibernate参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    jdbc基础和参考

    基于主键的一对一 Wife Husband id references Husband(id) id name name create table Husband( id number primary key, name varchar2(15) ); create table Wife( id number primary key references ...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子...

    精通Hibernate:对象持久化技术第二版part3

    7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子...

Global site tag (gtag.js) - Google Analytics