Hibernate級聯操作學習之持久化臨時對象
系統
1609 0
? 在實際程序開發中,經常需要持久化臨時對象,比如新建一個學生,將加入到一個存在的班級中(或者新建的一個班級),在這里不妨把引起級聯操作的對象稱為根對象(本文中所指的就是team對象)?,而根對象可能出于transient,persistence,detach三態,下面分別討論
首先,建立數據庫表:
?
CREATE
?
TABLE
?certificate?(
??id?
varchar
(
100
)?
NOT
?
NULL
?
default
?
''
,
??description?
varchar
(
100
)?
default
?
''
,
??
PRIMARY
?
KEY
??(id)
);
CREATE
?
TABLE
?student?(
??team_id?
varchar
(
100
)?
default
?
''
,
??id?
varchar
(
100
)?
NOT
?
NULL
?
default
?
''
,
??name?
varchar
(
20
)?
default
?
''
,
??cardId?
varchar
(
20
)?
NOT
?
NULL
?
default
?
''
,
??age?
int
(
11
)?
default
?
'
0
'
,
??
PRIMARY
?
KEY
??(id)
);
CREATE
?
TABLE
?team?(
??id?
varchar
(
100
)?
NOT
?
NULL
?
default
?
''
,
??teamName?
varchar
(
100
)?
default
?
''
,
??
PRIMARY
?
KEY
??(id)
);
INSERT
?
INTO
?certificate?
VALUES
?(
'
1
'
,
'
110108
'
);
INSERT
?
INTO
?certificate?
VALUES
?(
'
2
'
,
'
110109
'
);
INSERT
?
INTO
?student?
VALUES
?(
'
1
'
,
'
1
'
,
'
tomclus
'
,
'
2006m
'
,
33
);
INSERT
?
INTO
?student?
VALUES
?(
'
2
'
,
'
2
'
,
'
tom
'
,
'
2007m
'
,
22
);
INSERT
?
INTO
?team?
VALUES
?(
'
1
'
,
'
team1
'
);
?
持久化對象Pojo
?
package
?Cascade.Relation;
public
?
class
?Certificate?
...
{
????
private
?String?id;
????
private
?String?description;
????
private
?Student?stu;
????
public
?Student?getStu()?
...
{
????????
return
?stu;
????}
????
public
?
void
?setStu(Student?stu)?
...
{
????????
this
.stu?
=
?stu;
????}
????
public
?String?getDescription()?
...
{
????????
return
?description;
????}
????
public
?
void
?setDescription(String?description)?
...
{
????????
this
.description?
=
?description;
????}
????
public
?String?getId()?
...
{
????????
return
?id;
????}
????
public
?
void
?setId(String?id)?
...
{
????????
this
.id?
=
?id;
????}
}
package
?Cascade.Relation;
public
?
class
?Student?
...
{
????
private
?String?id;?
//
標識id
????
private
?String?cardId;?
//
學號
????
private
?String?name;?
//
學生姓名
????
private
?
int
?age;?
//
歲數
????
private
?Certificate?cer;
//
身分證
????
private
?Team?team;
//
班級
?????
public
?
int
?getAge()?
...
{
????????
return
?age;
????}
????
public
?String?getName()?
...
{
????????
return
?name;
????}
????
public
?String?getCardId()?
...
{
????????
return
?cardId;
????}
????
public
?
void
?setId(String?id)?
...
{
????????
this
.id?
=
?id;
????}
????
public
?
void
?setAge(
int
?age)?
...
{
????????
this
.age?
=
?age;
????}
????
public
?
void
?setName(String?stuName)?
...
{
????????
this
.name?
=
?stuName;
????}
????
public
?
void
?setCardId(String?cardId)?
...
{
????????
this
.cardId?
=
?cardId;
????}
????
public
?String?getId()?
...
{
????????
return
?id;
????}
????
public
?Student()?
...
{?
//
無參的構造函數
????}
?????
public
?Certificate?getCer()?
...
{
????????
return
?cer;
????}
????
public
?
void
?setCer(Certificate?pass)?
...
{
????????
this
.cer?
=
?pass;
????}
????
public
?Team?getTeam()?
...
{
????????
return
?team;
????}
????
public
?
void
?setTeam(Team?team)?
...
{
????????
this
.team?
=
?team;
????}
}
package
?Cascade.Relation;
import
?java.util.HashSet;
import
?java.util.Set;
public
?
class
?Team?
...
{
????
private
?String?id;
????
private
?Set?students
=
new
?HashSet();
????
private
?String?teamName;
????
private
?Set?tests;
??
????
public
?Set?getTests()?
...
{
????????
return
?tests;
????}
?
????
public
?
void
?setTests(Set?tests)?
...
{
????????
this
.tests?
=
?tests;
????}
Hibernate級聯操作學習之持久化臨時對象
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元