Hibernate級(jí)聯(lián)操作學(xué)習(xí)之持久化臨時(shí)對(duì)象
系統(tǒng)
1713 0
? 在實(shí)際程序開發(fā)中,經(jīng)常需要持久化臨時(shí)對(duì)象,比如新建一個(gè)學(xué)生,將加入到一個(gè)存在的班級(jí)中(或者新建的一個(gè)班級(jí)),在這里不妨把引起級(jí)聯(lián)操作的對(duì)象稱為根對(duì)象(本文中所指的就是team對(duì)象)?,而根對(duì)象可能出于transient,persistence,detach三態(tài),下面分別討論
首先,建立數(shù)據(jù)庫(kù)表:
?
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
'
);
?
持久化對(duì)象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;?
//
標(biāo)識(shí)id
????
private
?String?cardId;?
//
學(xué)號(hào)
????
private
?String?name;?
//
學(xué)生姓名
????
private
?
int
?age;?
//
歲數(shù)
????
private
?Certificate?cer;
//
身分證
????
private
?Team?team;
//
班級(jí)
?????
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()?
...
{?
//
無(wú)參的構(gòu)造函數(shù)
????}
?????
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級(jí)聯(lián)操作學(xué)習(xí)之持久化臨時(shí)對(duì)象
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元