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

Web框架
struts2(三)
struts2(四)
struts2(五)
struts2(六)
Spring中注入概念的简单介绍
深入探讨struts的ActionMessages消息机制(转)
DWR 的 Converter 实现原理简单分析及应用
详解Spring中bean的作用域
AJAX实现级联下拉框
Spring AOP面向方面编程原理:AOP概念
Spring结合Hibernate声明式事务配置
Struts 概述
Ajax 入门简介
Struts配置文件详解
Struts in Spring
Struts 开发的最佳实践
Struts核心标签
开发框架:利用STRUTS实现国际化支持
Struts自定义标签的过程
Struts 框架的基本概念及实现MVC模式的原理

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 765 ::
收藏到网摘: 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为空。