cascade级联,只会影响CRUD的CUD,不会影响读取。不设置级联,从多的一方能读出一的一方,设了级联,从一的一方,默认也不能读出多的一方。

如果两个对象之间有关联,不管是一对多,多对一,单向还是双向,如果从A可以导向到B:

A--->B

默认情况下对A的保存不会影响到B,除非设cascade。如果A--->B--->C,从A能导航到B,B能导航到C,在B上也加上级联,对A的操作就能影响到C,但是,级联不是必须的,它只是让编程稍有方便。完全可以手动先存C,再存B,再存A.。

例子:Group类:

package com.oracle.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_group")//group是mysql的关键字,换个名
public class Group { private int id;
private String name; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

User类:

package com.oracle.hibernate;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; @Entity
public class User { private int id;
private String name;
private Group group; //多对一,级联。
    @ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="groupId")//指定外键名称
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

测试:

package com.oracle.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass; public class Test { private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){ try {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @org.junit.Test
public void testSchemaExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }
@AfterClass
public static void afterClass(){
sf.close();
} @org.junit.Test
public void testSave(){ Session s = sf.getCurrentSession();
s.beginTransaction();
User user = new User();
user.setName("u1");
Group g = new Group();
g.setName("g1");
user.setGroup(g);
//s.save(g);默认不会自动保存关联变量,
//设置cascade后,直接保存user就能把user的group也保存
s.save(user);
s.getTransaction().commit();
} }

数据库:

最新文章

  1. Android中处理崩溃异常
  2. svn相关知识点
  3. 汇总常用的jQuery操作Table tr td方法
  4. [Tool] 配置文件之Web.config
  5. wireshark 和 Httpwatch tcpdump
  6. Visual.Studio.2013.IDE+visual.studio.15.preview5 编译器
  7. 16.(转) Android之Support v4、v7、v13的区别和应用场景
  8. PHP的GD库函数大全
  9. 第26讲 对话框AlertDialog的自定义实现
  10. Linux学习--alias命令
  11. ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)
  12. CentOS 6.5 配置 SSDB 1.8.0
  13. 略过 Mysql 5.7的密码策略
  14. OC的内存管理(二)ARC
  15. maven 将第三方jar包转成maven的jar包
  16. 微信跳转外部浏览器下载app
  17. Ubuntu16.04 apt源更新
  18. Partition by使用
  19. 详细介绍jQuery.outerWidth() 函数具体用法
  20. scroll滚动动画(js/ts)

热门文章

  1. 如何把asp.net上的服务在iis调试
  2. mysql导入数据load data infile用法(转)
  3. 查看jar包的jdk版本并降级
  4. SQL SERVER 查找锁信息
  5. echarts重绘
  6. Katalon Studio简单使用(二)
  7. django drf 权限permission
  8. AI下载步骤
  9. JAVA判断质数
  10. python+selenium 定位隐藏元素