如果没有关联关系,平常我们使用save、get、update、delete方法即可对实体对象进行增删改查,但是一旦实体对象有关联关系的时候,就不能直接使用save、get、update、delete方法了,就要进行一系列的语句拆分和组合。
牵扯到对一个实体对象做操作,是否要对关联的另外一个实体对象做操作。
这里来探讨一下删除delete“多对一”中“一”这一方的问题:
User.java:
Group.java:
进行删除的测试:
对于删除,应该先select之后才删除,因为要确定是否存在此数据。
当然如果已经知道id了,那就可以使用id删除字段。
测试代码:
输出的sql语句:
Hibernate:
select
user0_.id as id1_1_,
user0_.group_id as group3_1_1_,
user0_.name as name1_1_,
group1_.id as id0_0_,
group1_.name as name0_0_
from
m_user user0_
left outer join
m_group group1_
on user0_.group_id=group1_.id
where
user0_.id=?
Hibernate:
select
users0_.group_id as group3_0_1_,
users0_.id as id1_,
users0_.id as id1_0_,
users0_.group_id as group3_1_0_,
users0_.name as name1_0_
from
m_user users0_
where
users0_.group_id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_group
where
id=?
发现,u3、u4、u5、u6都被删除了,group也被删除了。(u3、u4、u5、u6都属于group)
原因是因为我们删除u3(cascade={CascadeType.ALL})的时候,u3关联的是g1,而g1里面也是cascade={CascadeType.ALL},g1又关联u4、u5、u6,所以u4、u5、u6也被删除了。
那怎么办?我们可以把CascadeType设为其他的,不设置为ALL,但是会影响到其他,有没有其他办法?
在你不方便设置cascade的情况下,办法是“打破关联关系”。说白了就是你手动去除u3和g1的关联关系:
输出的sql语句为:
Hibernate:
select
user0_.id as id1_1_,
user0_.group_id as group3_1_1_,
user0_.name as name1_1_,
group1_.id as id0_0_,
group1_.name as name0_0_
from
m_user user0_
left outer join
m_group group1_
on user0_.group_id=group1_.id
where
user0_.id=?
Hibernate:
delete
from
m_user
where
id=?
发现只删除了user3,并没有删除级联的group。
还有一中办法,就是用HQL来删除。
代码:
输出的sql语句:
Hibernate:
delete
from
m_user
where
id=3
牵扯到对一个实体对象做操作,是否要对关联的另外一个实体对象做操作。
这里来探讨一下删除delete“多对一”中“一”这一方的问题:
User.java:
package cn.edu.hpu.one2many; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="m_user") public class User { private int id; private String name; private Group group; //只要有双向就要指定制定一个属性(mapedby) //不指定的话会有两个相同的字段产生 @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER) 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; } }
Group.java:
package cn.edu.hpu.one2many; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="m_group") public class Group { private int id; private String name; private Set<User> users=new HashSet<User>(); //选择set的原因是因为,set互相之间不会有重复的 //跟数据库模型比较匹配 @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; } @OneToMany(mappedBy="group", cascade={CascadeType.ALL}, fetch=FetchType.LAZY ) public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
进行删除的测试:
对于删除,应该先select之后才删除,因为要确定是否存在此数据。
当然如果已经知道id了,那就可以使用id删除字段。
测试代码:
@Test public void testDeleteUser(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); User u=(User)s.load(User.class,3); s.delete(u); s.getTransaction().commit(); }
输出的sql语句:
Hibernate:
select
user0_.id as id1_1_,
user0_.group_id as group3_1_1_,
user0_.name as name1_1_,
group1_.id as id0_0_,
group1_.name as name0_0_
from
m_user user0_
left outer join
m_group group1_
on user0_.group_id=group1_.id
where
user0_.id=?
Hibernate:
select
users0_.group_id as group3_0_1_,
users0_.id as id1_,
users0_.id as id1_0_,
users0_.group_id as group3_1_0_,
users0_.name as name1_0_
from
m_user users0_
where
users0_.group_id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_user
where
id=?
Hibernate:
delete
from
m_group
where
id=?
发现,u3、u4、u5、u6都被删除了,group也被删除了。(u3、u4、u5、u6都属于group)
原因是因为我们删除u3(cascade={CascadeType.ALL})的时候,u3关联的是g1,而g1里面也是cascade={CascadeType.ALL},g1又关联u4、u5、u6,所以u4、u5、u6也被删除了。
那怎么办?我们可以把CascadeType设为其他的,不设置为ALL,但是会影响到其他,有没有其他办法?
在你不方便设置cascade的情况下,办法是“打破关联关系”。说白了就是你手动去除u3和g1的关联关系:
@Test public void testDeleteUser(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); User u=(User)s.load(User.class,3); u.setGroup(null); s.delete(u); s.getTransaction().commit(); }
输出的sql语句为:
Hibernate:
select
user0_.id as id1_1_,
user0_.group_id as group3_1_1_,
user0_.name as name1_1_,
group1_.id as id0_0_,
group1_.name as name0_0_
from
m_user user0_
left outer join
m_group group1_
on user0_.group_id=group1_.id
where
user0_.id=?
Hibernate:
delete
from
m_user
where
id=?
发现只删除了user3,并没有删除级联的group。
还有一中办法,就是用HQL来删除。
代码:
@Test public void testDeleteUser(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); s.createQuery("delete from User u where u.id = 3").executeUpdate(); s.getTransaction().commit(); }
输出的sql语句:
Hibernate:
delete
from
m_user
where
id=3
说明只删除了u3.有的是办法= =
尊重开源精神,尊重劳动成果,转载请注明出处:http://blog.csdn.net/acmman/article/details/43889419