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 (?, ?)
分享到:
相关推荐
<hibernate-mapping> <!-- 其他属性映射... --> <one-to-one name="passport" class="com.example.Passport" cascade="all"/> </hibernate-mapping> ``` `Passport.hbm.xml`: ```xml <hibernate-...
</hibernate-mapping> ``` 为了将 `Publisher` 类添加到我们的应用程序中,还需要在 Hibernate 配置文件中指定它: ```xml <mapping resource="com/metaarchit/bookshop/Publisher.hbm.xml"/> ``` ##### 2. Book ...
例如,`<one-to-one>`、`<many-to-one>`、`<one-to-many>`和`<many-to-many>`元素分别对应这四种关系。关联映射通常涉及外键的处理和级联操作。 5. 注解方式的Mapping 除了XML映射,Hibernate还支持注解方式的映射...
本实例将详细讲解如何在Hibernate中实现Many-to-One关系映射,这是一种常见的数据库关联,表示一个实体可以与多个其他实体相关联。 在Many-to-One关系中,通常一个实体(如部门)可以有多个相关实体(如员工),而...
<hibernate-mapping> <!-- ...其他属性映射... --> <one-to-many class="Order"/> </hibernate-mapping> <hibernate-mapping> <!-- ...其他属性映射... --> </hibernate-mapping> ``` 在这里,`...
本程序包含: hibenate 上传图片,二进制数据,大文本,集合映射的多种关系,onetoone,ontomany,manytomany等关系详细代码示例以及注释,全部由本人测试通过,对理解hibernate配置有极大帮助。
<hibernate-mapping> </hibernate-mapping> <hibernate-mapping> <!-- 映射关联PO,column是Address在该表中的外键列名,增加unique变成“1-1” --> <many-to-one name="address" column...
Hibernate 是一个开源的对象关系映射...通过深入学习这份Hibernate-api文档,开发者能够熟练掌握Hibernate的核心概念、配置、查询机制以及对象关系映射,从而在实际项目中更加自如地运用Hibernate这一强大的ORM工具。
10. **多对一(One-to-Many)、一对多(Many-to-One)、一对一(One-to-One)、多对多(Many-to-Many)关系**: Hibernate支持这四种数据库关联关系,通过不同的注解配置来实现。 **使用Hibernate的步骤** 1. **...
<hibernate-mapping> <class name="com.bjsxt.hibernate.User" table="t_user"> <!-- <many-to-one name="group" column="groupid" cascade="all"/> --> <many-to-one name="group" column...
<hibernate-mapping> <!-- 其他属性 --> <one-to-one name="account" class="com.example.Account" foreign-key="fk_user_account"/> ``` 在这个配置中,`name`属性指定了在`User`类中的属性名,`class`属性...
Spring提供了IOC(Inversion of Control)和AOP(Aspect Oriented Programming)功能,而Hibernate则是一个强大的ORM(Object-Relational Mapping)工具,它们降低了对EJB的依赖。 2. **Web层技术**:书中可能会...
接下来,我们转向`hibernate-mapping-3.0.dtd`,它是Hibernate实体映射文件的DTD。映射文件是将Java类与数据库表关联起来的关键,通过这些映射,Hibernate能够理解如何将对象状态转化为数据库记录,反之亦然。在这个...
<hibernate-mapping> <!-- ... --> <one-to-one name="driverLicense" class="com.example.DriverLicense" /> <!-- ... --> </hibernate-mapping> ``` **六、注意事项** 1. 为了确保一对一...
《深入理解Hibernate:第三解——Many-to-One关系映射实战》 在Java世界里,ORM(Object-Relational Mapping)框架的出现极大地简化了数据库与Java对象之间的交互,而Hibernate作为其中的佼佼者,更是备受开发者青睐...
在Java世界中,Hibernate是一个非常流行的Object-Relational Mapping(ORM)框架,它极大地简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据库事务。本示例将详细讲解如何在Hibernate中实现多对多...
<hibernate-mapping> <one-to-one name="identityCard" class="com.example.IdentityCard" constraint="unique"/> </hibernate-mapping> <!-- 身份证的映射文件 --> <hibernate-mapping> ...
<hibernate-mapping> <class name="com.suxiaolei.hibernate.pojos.Customer" table="customer"> <!-- 主键设置 --> <column name="id"></column> <generator class="uuid"></generator> <!-- 属性设置 -->...
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="包名"> 类名" table="表名"> ...