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

Web框架
Web框架介绍---SpringMVC
基于Django框架的敏捷Web开发
驾驭新的Ruby Web框架Waves
JAVA脚本取得struts2 OgnlValueStack中的值
Struts中DownloadAction的使用
优化Hibernate性能的几点建议
Hibernate高级查询实战
对于Struts和Spring两种MVC框架的比较
JSF与Struts的比较 超易懂!
hibernate 主键生成方式
spring acegi 官方例子
获取ApplicationContext的几种方式
Spring与自动调度任务 基于Timer的任务调度器的应用
spring+hibernate 的包的详解,帮你了解每个包的作用以及是否必要导入工程
hibernate 一对一(one to one)级联保存
struts formbean 就是鸡肋
Struts中常用的几种Action
spring的应用事例
struts2(一)
struts2(二)

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


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