读书人

one-to-one 地图ping of hibernate

发布时间: 2012-10-25 10:58:57 作者: rapoo

one-to-one mapping of hibernate

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 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 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" 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 (?, ?)

读书人网 >软件架构设计

热点推荐