Person类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.bjsxt.hibernate;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity public class Person {
private Integer id;
private String name;
private Group1 group1;
@ManyToOne (cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
public Group1 getGroup1() {
return group1;
}
public void setGroup1(Group1 group1) {
this .group1 = group1;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
} |
Group1类(数据库中group是关键字,也可以做一个映射):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.bjsxt.hibernate;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity public class Group1 {
private Integer id;
private String name;
private List<Person> persons;
@OneToMany (mappedBy= "group1" ,fetch=FetchType.EAGER,cascade={CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this .persons = persons;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
} |
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package com.bjsxt.hibernate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Test;
public class TestPerson_group {
private static SessionFactory sessionFactory;
@Test
public void testAddPerson(){
sessionFactory= new AnnotationConfiguration().configure().buildSessionFactory();
Person person1= new Person();
person1.setName( "person1" );
Person person2= new Person();
person2.setName( "person2" );
Group1 group1= new Group1();
group1.setName( "group1" );
List<Person> persons= new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
person1.setGroup1(group1);
person2.setGroup1(group1);
group1.setPersons(persons);
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
session.persist(group1);
session.getTransaction().commit();
}
@Test
public void deleteGroup1(){
sessionFactory= new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Group1 group1=(Group1) session.get(Group1. class , 1 );
List<Person> persons=group1.getPersons();
Iterator<Person> iterator=persons.iterator();
if (iterator.hasNext()){
Person person=iterator.next();
System.out.println(person.getName());
person.setGroup1( null );
}
session.update(group1);
/*
* 一定要先update一下group1,
* 将group1的每一个user的group设置为null,同步到数据库,否则group1对应于person的关联关系没有打破*/
group1.setPersons(null);
session.delete(group1);
session.getTransaction().commit();
System.out.println(group1.getName());
}
@Test
public void deleteUser(){
sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
/*Person person=(Person) session.get(Person.class, 3);
person.setGroup1(null);
session.delete(person);*/
/*
* 或者直接写HQL删除*/
session.createQuery( "delete from Person p where p.id=4" ).executeUpdate();
session.getTransaction().commit();
}
@Test
public void updatePerson(){
sessionFactory= new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Person person=(Person) session.get(Person. class , 1 );
person.setGroup1( null );
session.update(person);
session.getTransaction().commit();
}
@Test
public void updateGroup1(){
sessionFactory= new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Group1 group1=(Group1) session.get(Group1. class , 1 );
group1.setPersons( null );
session.update(group1);
session.getTransaction().commit();
}
}
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1879737