hibernate双向一对一
|
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
|
package
com.bjsxt.hibernate.oneToOne;
import
javax.persistence.CascadeType;
import
javax.persistence.Entity;
import
javax.persistence.FetchType;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.JoinColumn;
import
javax.persistence.OneToOne;
import
com.bjsxt.hibernate.oneToOne.PersonInfo;
@Entity
public
class
Person {
private
int
id;
private
String name;
private
String password;
private
PersonInfo personInfo;
@Id
@GeneratedValue
public
int
getId() {
return
id;
}
@Override
public
String toString() {
return
"Person [id="
+ id +
", name="
+ name +
", password="
+ password
+
", PersonInfo="
+ personInfo +
"]"
;
}
public
Person(
int
id, String name, String password,
com.bjsxt.hibernate.oneToOne.PersonInfo personInfo) {
super
();
this
.id = id;
this
.name = name;
this
.password = password;
this
.personInfo = personInfo;
}
public
Person( String name, String password,
com.bjsxt.hibernate.oneToOne.PersonInfo personInfo) {
super
();
this
.name = name;
this
.password = password;
this
.personInfo = personInfo;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
Person() {
super
();
// TODO Auto-generated constructor stub
}
public
String getName() {
return
name;
}
@OneToOne
(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn
(name=
"personInfo"
)
public
PersonInfo getPersonInfo() {
return
personInfo;
}
public
void
setPersonInfo(PersonInfo personInfo) {
this
.personInfo = personInfo;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
}
|
|
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
|
package
com.bjsxt.hibernate.oneToOne;
import
javax.persistence.CascadeType;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.OneToOne;
@Entity
public
class
PersonInfo {
private
int
id;
private
String name;
private
int
age;
private
Person person;
@OneToOne
(mappedBy=
"personInfo"
,cascade=CascadeType.ALL)
public
Person getPerson() {
return
person;
}
public
void
setPerson(Person person) {
this
.person = person;
}
public
PersonInfo( String name,
int
age) {
super
();
this
.name = name;
this
.age = age;
}
public
PersonInfo(
int
id, String name,
int
age) {
super
();
this
.id = id;
this
.name = name;
this
.age = age;
}
public
PersonInfo() {
super
();
// TODO Auto-generated constructor stub
}
public
int
getAge() {
return
age;
}
@Id
@GeneratedValue
public
int
getId() {
return
id;
}
public
String getName() {
return
name;
}
public
void
setAge(
int
age) {
this
.age = age;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
void
setName(String name) {
this
.name = name;
}
@Override
public
String toString() {
return
"PersonInfo [id="
+ id +
", name="
+ name +
", age="
+ age +
"]"
;
}
}
|
|
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
|
package
com.bjsxt.hibernate;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.AnnotationConfiguration;
import
org.hibernate.tool.hbm2ddl.SchemaExport;
import
org.junit.BeforeClass;
import
com.bjsxt.hibernate.oneToOne.Person;
import
com.bjsxt.hibernate.oneToOne.PersonInfo;
public
class
OneToOneTest {
private
static
SessionFactory sessionFactory;
@BeforeClass
public
static
void
beforeClass() {
new
SchemaExport(
new
AnnotationConfiguration().configure()).create(
false
,
true
);
sessionFactory =
new
AnnotationConfiguration().configure().buildSessionFactory();
}
public
static
void
main(String[] args) {
//AddPerson();
//getPerson();
//delPerson();
updatePerson();
}
public
static
void
getPerson(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Person person=(Person) session.get(Person.
class
,
1
);
System.out.println(person.toString());
session.getTransaction().commit();
}
public
static
void
delPerson(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Person person=(Person) session.get(Person.
class
,
1
);
session.delete(person);
System.out.println(person.toString());
session.getTransaction().commit();
}
public
static
void
updatePerson(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Person person=(Person) session.get(Person.
class
,
2
);
//先get数据,不写session.update(person)数据库也更新了记录
person.setName(
"王五"
);
//级联的数据直接修改,也不用update,hibernate自动完成修改
PersonInfo personInfo=person.getPersonInfo();
personInfo.setName(
"王五"
);
System.out.println(person.toString());
session.getTransaction().commit();
}
public
static
void
AddPerson(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
PersonInfo personInfo=
new
PersonInfo(
"张三"
,
23
);
Person person=
new
Person(
"张三"
,
"2"
, personInfo);
personInfo.setPerson(person);
//session.save(personInfo);
session.save(person);
session.getTransaction().commit();
}
public
static
void
delPersonByPersonInfo(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Person person=(Person) session.get(Person.
class
,
2
);
session.delete(person.getPersonInfo());
System.out.println(person.toString());
session.getTransaction().commit();
}
public
static
void
AddPersonByPersonInfo(){
sessionFactory=
new
AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
PersonInfo personInfo=
new
PersonInfo(
"张三"
,
23
);
Person person=
new
Person(
"张三"
,
"2"
, personInfo);
personInfo.setPerson(person);
session.save(personInfo);
//session.save(person);
session.getTransaction().commit();
}
}
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1876719