因为时间关系,我在这里测试的环境是一对多的关系里面用到的注解方式的级联,网上也有很多贴子,我也看过了,但是呢,我还是自己总结一下吧,这觉得级联是单向的,不是双向的,意思就是说,我们在设置两个类的对象之间关系的时候,总是在一方设置的很具体,在另外一方设置一个mappedBy即可,但是如果想要两边都能删除的时候,或者在生成的时候,必须在两边都设置cascade=CascadeType.All才有效果,下面是测试代码,测试样例是参考马士兵的视频做的,
package com.jll.model; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToMany(mappedBy="group",cascade=CascadeType.ALL) public Set<User> getUsers() { return users; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUsers(Set<User> users) { this.users = users; } }
现在这里有cascade=CascadeType.ALL。在相关联的类的另一边同样也有,
package com.jll.model; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "t_user") public class User { private int id; private String name; private Group group; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="groupId") public Group getGroup() { return group; } @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } public void setGroup(Group group) { this.group = group; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public String toString(){ return this.getName()+"---"+this.getId()+"---"+this.getGroup().getId(); } }
测试代码:
package com.jll.model; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; public class TestCoreAPI { private static SessionFactory sf=null; private static Configuration configuration = new Configuration().configure(); @BeforeClass public static void beforeClass(){ StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(). applySettings(configuration.getProperties()); sf = configuration.buildSessionFactory(builder.build()); } @AfterClass public static void afterClass(){ sf.close(); } @Test public void testRelationShip(){ SchemaExport se = new SchemaExport(configuration); se.create(true, true); Session session = sf.getCurrentSession(); Group g = new Group(); g.setName("group1"); User u1 = new User(); User u2 = new User(); /*u1.setGroup(g); u2.setGroup(g);*/ u1.setName("u1"); u2.setName("u2"); Set<User> users = new HashSet<User>(); users.add(u1); users.add(u2); g.setUsers(users); session.beginTransaction(); session.save(g); session.getTransaction().commit(); }
生成的SQL语句如下:
alter table t_user drop foreign key FK_7ktm6l2qkykpqrf6oq01ys8wy drop table if exists t_group drop table if exists t_user create table t_group ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_user ( id integer not null auto_increment, name varchar(255), groupId integer, primary key (id) ) alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id) Hibernate: insert into t_group (name) values (?) Hibernate: insert into t_user (groupId, name) values (?, ?) Hibernate: insert into t_user (groupId, name) values (?, ?)
如果Group类没有加上级联的话,生成的语句如下:
alter table t_user drop foreign key FK_7ktm6l2qkykpqrf6oq01ys8wy drop table if exists t_group drop table if exists t_user create table t_group ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_user ( id integer not null auto_increment, name varchar(255), groupId integer, primary key (id) ) alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id) Hibernate: insert into t_group (name) values (?)
这里只插入了一次,而上面的那个插入了三次,所以我猜测级联是单向的,不是双向的,如果想要两边都可以进行crud,则被关联的类都要加上cascade=CascadeType.ALL,我也进行了删除测试,删除的时候必须先查出来,然后才能进行级联删除,得出来的结论也是与上面的实验一样,在这里就不贴代码了。