`
tanglei198577
  • 浏览: 58357 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

one-to-one mapping of hibernate

    博客分类:
  • SSH
阅读更多

first ,create two tables you want to make the OR mapping.

create table T_PASSPORT
(
  ID     NUMBER(4) not null primary key,
  SERIAL VARCHAR2(30) not null
)
create table T_PERSON
(
  ID   NUMBER(4) not null primary key,
  NAME VARCHAR2(30) not null,
  AGE  NUMBER(4) not null
)

alter table T_PASSPORT
  add constraint FK_ID foreign key (ID)
  references T_PERSON (ID) on delete cascade;

 

and the hbm.xml of these two tables are:

<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="Reference.TPassport" table="T_PASSPORT" schema="JIL">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="4" scale="0" />
            <generator class="foreign">
              <param name="property">TPerson</param>
            </generator>
        </id>
        <one-to-one name="TPerson" class="Reference.TPerson" constrained="true">
        </one-to-one>
        <property name="serial" type="java.lang.String">
            <column name="SERIAL" length="30" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

 

<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="Reference.TPerson" table="T_PERSON" schema="JIL">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="4" scale="0" />
            <generator class="native"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="30" not-null="true" />
        </property>
        <property name="age" type="java.lang.Long">
            <column name="AGE" precision="4" scale="0" not-null="true" />
        </property>        
        <one-to-one name="passport" class="Reference.TPassport" cascade="all" outer-join="true"></one-to-one>
    </class>
</hibernate-mapping>

 Test code:

TPerson per = new TPerson();
TPassport passport = new TPassport();
per.setName("haha");
per.setAge(new Long(20));
passport.setSerial("123345");
passport.setTPerson(per);
per.setPassport(passport);
session.save(per);

 then output is :

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into JIL.T_PERSON (NAME, AGE, ID) values (?, ?, ?)
Hibernate: insert into JIL.T_PASSPORT (SERIAL, ID) values (?, ?)

0
0
分享到:
评论

相关推荐

    Hibernate集合关系映射及one-one,one-many,many-many示例(带注释)

    本程序包含: hibenate 上传图片,二进制数据,大文本,集合映射的多种关系,onetoone,ontomany,manytomany等关系详细代码示例以及注释,全部由本人测试通过,对理解hibernate配置有极大帮助。

    Hibernate Recipes A Problem-Solution Approach

    Hibernate continues to be the most popular out-of-the-box framework solution for Java Persistence and data/database accessibility techniques and patterns. It is used for e-commerce–based web ...

    Hibernate Mapping Many-to-One 实例 内附源代码及附件下载

    NULL 博文链接:https://liqita.iteye.com/blog/1497077

    hibernate关系映射配置.rar

    &lt;hibernate-mapping&gt; &lt;class name="com.bjsxt.hibernate.User" table="t_user"&gt; &lt;!-- &lt;many-to-one name="group" column="groupid" cascade="all"/&gt; --&gt; &lt;many-to-one name="group" column...

    hibernate总结

    &lt;hibernate-mapping&gt; &lt;class name="org.wllt.www.po.Dept " table="dept " catalog="hibernate"&gt; &lt;set name="emps" inverse ="true" lazy="true" cascade="none" batch-size=...

    Lazy loading - eager loading

    &lt;/hibernate-mapping&gt;and finally the mapping for the OrderLine entity. &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" ...

    Hibernate Reference Documentation3.1

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. Hi/lo algorithm 5.1.4.3. UUID algorithm 5.1.4.4. Identity columns and sequences 5.1.4.5. Assigned identifiers 5.1.4.6. ...

    HibernateAPI中文版.chm

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    hibernate3.2中文文档(chm格式)

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    英语资料,很有用的哦

    &lt;hibernate-mapping&gt; &lt;class name="com.hibernate.model.SystemClassInfo" table="system_class_info" schema="dbo" catalog="db_school"&gt; &lt;many-to-one name="systemSpecialtyCode" class=...

    Hibernate+中文文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate中文API大全

    (A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class.) 接下来的的例子是从Order到Item的一个多对多的关联关系, 关联属性是 purchaseDate, ...

    Hibernate中文详细学习文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    最全Hibernate 参考文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    Hibernate 中文 html 帮助文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

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

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    Hibernate教程

    6.1.2. hibernate-mapping 6.1.3. class 6.1.4. id 6.1.4.1. Generator 6.1.4.2. 高/低位算法(Hi/Lo Algorithm) 6.1.4.3. UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and ...

    hibernate 3中的缓存小结

    DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;!— 配置缓存,必须紧跟在class元素...

    Hibernate3+中文参考文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

Global site tag (gtag.js) - Google Analytics