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