来源我的百度空间http://hi.baidu.com/nyistzengpan
(1)ManyToOne(多对一)单向:不产生中间表,但可以用@Joincolumn(name=" ")来指定生成外键的名字,外键在多的一方表中产生!
(2)OneToMany(一对多)单向:会产生中间表,此时可以用@onetoMany @Joincolumn(name=" ")避免产生中间表,并且指定了外键的名字(别看@joincolumn在一中写着,但它存在在多的那个表中)
(3)OneToMany ,ManyToOne 双向(两个注解一起用的):如果不在@OneToMany中加mappedy属性就会产生中间表,此时通常在@ManyToOne的注解下再添上注解@Joincolumn(name=" ")来指定外键的名字(说明:多的一方为关系维护端,关系维护端负责外键记录的更新,关系被维护端没有权利更新外键记录)!(@OneToMany(mappedBy="一对多中,多中一的属性")出现mapby为被维护端|||默认为延迟加载)
ps:举例说明:以下依次是一对多的关系(course----testtopic,chapter----testtopic),一共四个表(搭的都是双向关系),时间关系最后一个不上代码了
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
106
107
108
109
110
111
112
113
114
115
|
@Entity
//科目表
public
class
Course {
private
int
id;
private
String name;
private
Set<Chapter> chapters =
new
HashSet<Chapter>();
public
void
addChapters(Chapter chapter){
if
(!
this
.chapters.contains(chapter)){
this
.chapters.add(chapter);
chapter.setCourse(
this
);
}
}
@OneToMany
(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy=
"course"
)
public
Set<Chapter> getChapters() {
return
chapters;
}
public
void
setChapters(Set<Chapter> chapters) {
this
.chapters = chapters;
}
@Id
@GeneratedValue
(strategy=GenerationType.AUTO)
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
@Column
(length =
50
)
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
}
@Entity
//章节表
public
class
Chapter {
private
int
id;
private
String name;
private
Set<TestTopic> topics =
new
HashSet<TestTopic>();
private
Course course;
@Id
@GeneratedValue
(strategy =GenerationType.AUTO )
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
@Column
(length =
150
)
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
@ManyToOne
@JoinColumn
(name=
"courseId"
)
public
Course getCourse() {
return
course;
}
public
void
setCourse(Course course) {
this
.course = course;
}
@OneToMany
(cascade=CascadeType.REFRESH,mappedBy=
"chapter"
,fetch=FetchType.EAGER)
public
Set<TestTopic> getTopics() {
return
topics;
}
public
void
setTopics(Set<TestTopic> topics) {
this
.topics = topics;
}
}
@Entity
//考试套题表
public
class
TestTopic {
private
int
id;
private
String name;
private
Set<Question> questions =
new
HashSet<Question>();
private
Chapter chapter;
@Id
@GeneratedValue
(strategy =GenerationType.AUTO )
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
@Column
(length =
150
)
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
@OneToMany
(cascade=CascadeType.ALL,mappedBy=
"testTopic"
,fetch=FetchType.EAGER)
public
Set<Question> getQuestions() {
return
questions;
}
public
void
setQuestions(Set<Question> questions) {
this
.questions = questions;
}
@ManyToOne
@JoinColumn
(name=
"chapterId"
)
public
Chapter getChapter() {
return
chapter;
}
public
void
setChapter(Chapter chapter) {
this
.chapter = chapter;
}
}
|