当前位置: 首页 > 图文教程 > Java技术 > Web框架 > hibernate 一对一(one to one)级联保存

Web框架
Web框架:addOptions and removeAllOptions
Web框架:Xfire与Spring集成那些事
Web框架:多个dwr.xml配置方法
Web框架:小编整理Hibernate 基本查詢
Web框架:DWR使用中的web.xml配置
Web框架:Struts2使用Spring插件完成整合
Web框架:小编叙Spring的事务管理
Web框架:Struts2国际化实现用户自行选择语言
Web框架:Struts2中加载资源文件的方式
Web框架:Struts2中整合图表工具JFreeChart的时间顺序图
Web框架:浅谈Struts2的内建校验器
Web框架:FreeMarker中的escape , noescape指令
Struts2的Visitor校验器
Struts2中的subset标签使用方法浅谈
Hibernate核心接口那些事
Spring中的依赖注入
Spring中的Inversion of Control 容器
浅析Spring中的单元测试
用StrutsTestCase测试Struts应用程序
浅谈Struts中html:options的使用

Web框架 中的 hibernate 一对一(one to one)级联保存


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 769 ::
收藏到网摘: n/a

前段时间用到了一对一级联保存的情况,在网上查了好多资料,现在主要总结如下:

 

一  : 一般说一对一都有两种情况,一种是两张表使用同一主键,我现在说的是第二种外键关联的情况;

 

两个表Student, card  Student的主键是是card 的唯一外键,不多说了,配置如下:

1   Student 主表映射文件中:

<one-to-one name="card" class="com.xxx.entity.bean.Card" property-ref="stuid" cascade="save-update"></one-to-one>

card 表映射文件中:

<many-to-one name="student" class="com.xxx.entity.bean.Student" unique="true" outer-join="true" cascade="save-update">
            <column name="stuid" length="20" not-null="true" />
     </many-to-one>

2 bean类

public class Card implements java.io.Serializable {

 private Student student;

get,set方法

}

 

public class Card implements java.io.Serializable {

 private Card card;

get,set方法

}

 

3 action保存

 

Student stu = new Student();

Card card = new Card();

stu.setCard(card);

card.setStudent(stu);

dao.save(stu);

这样就可以级联保存card了

我这里只是大体写了点,当然应该还有其他的属性

 

 

4 注意:

 

在card类里只可以有student的对象属性,不可以有简单属性

像这样 private String stuid; 如果这样配的话

<many-to-one name="student" class="com.xxx.entity.bean.Student" unique="true" outer-join="true" cascade="save-update" insert="false" update="false">
            <column name="stuid" length="20" not-null="true" />
     </many-to-one>

就必须要加上里面的红色部分,而且级联插入的时候card的stuid为空。