亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Hibernate集合映射之List

系統 1583 0

使用List和Map有很多共性,比如說不能再1方設置inverse="true"

不同的是,list可以保留元素的順序,這個使通過一個idx字段來實現的,比如說Team和Student的1對多關系,我們給team加入學生的時候,需要記錄加入的順序,這時候我們可以使用list方式,并在student表中新增加一個idx字段(但在Student實體類中不定義idx屬性)?

數據庫結構:

?

create ? table ?teamList?(id? varchar ( 32 ),teamname? varchar ( 32 ));
create ? table ?studentList?(id? varchar ( 32 ),cardid? varchar ( 32 ),name? varchar ( 32 ),age? int ,description? varchar ( 32 ),?team_id? varchar ( 32 ),idx? int );

?

POJO

?

package ?Collection.List;

import ?java.util.ArrayList;
import ?java.util.List;
import ?java.util.Map;

public ? class ?Team? ... {
???
private ?String?id;
???
private ?String?teamname;
???
private ?List?students = new ?ArrayList();
public ?String?getId()? ... {
????
return ?id;
}

public ? void ?setId(String?id)? ... {
????
this .id? = ?id;
}

public ?String?getTeamname()? ... {
????
return ?teamname;
}

public ? void ?setTeamname(String?teamname)? ... {
????
this .teamname? = ?teamname;
}

public ?List?getStudents()? ... {
????
return ?students;
}

public ? void ?setStudents(List?students)? ... {
????
this .students? = ?students;
}


}




package ?Collection.List;

public ? class ?Student? ... {
??
private ?String?id;
??
private ?String?name;
??
private ?String?description;
??
private ? int ?age;
??
private ?String?cardid;
??
private ?Team?team;
public ?String?getId()? ... {
????
return ?id;
}

public ? void ?setId(String?id)? ... {
????
this .id? = ?id;
}

public ?String?getName()? ... {
????
return ?name;
}

public ? void ?setName(String?name)? ... {
????
this .name? = ?name;
}

public ?String?getDescription()? ... {
????
return ?description;
}

public ? void ?setDescription(String?description)? ... {
????
this .description? = ?description;
}

public ? int ?getAge()? ... {
????
return ?age;
}

public ? void ?setAge( int ?age)? ... {
????
this .age? = ?age;
}

public ?String?getCardid()? ... {
????
return ?cardid;
}

public ? void ?setCardid(String?cardid)? ... {
????
this .cardid? = ?cardid;
}

public ?Team?getTeam()? ... {
????
return ?team;
}

public ? void ?setTeam(Team?team)? ... {
????
this .team? = ?team;
}

}

?

Team.hbm.xml

?

<? xml?version="1.0"?encoding="utf-8" ?>
<! DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!-- ?
????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
-->
< hibernate-mapping >
< class? name ="Collection.List.Team" ?table ="teamList" ? >
????
< id? name ="id" ?unsaved-value ="null" >
??????
< generator? class ="uuid.hex" ></ generator >
????
</ id >
????
< property? name ="teamname" ?type ="string" ?column ="teamname" ></ property >
????
<!-- 由于index需要team來維護,所以,使用list表示1對多時候,不能再1方設置inverse="true" -->
????
< list? name ="students" ?table ="studentList" ?cascade ="all" ? >
??????
< key? column ="team_id" ></ key >
??????
<!-- ?index代表key,element(或one-to-many)代表value -->
??????
<!-- ?這里用cardid作為key,具體的student實例作為value? -->
??????
< index? column ="idx" ?type ="int" ></ index >
??????
< one-to-many? class ="Collection.List.Student" />
????
</ list >
</ class >

</ hibernate-mapping >

?

Student.hbm.xml

?

<? xml?version="1.0"?encoding="utf-8" ?>
<! DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!-- ?
????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
-->
< hibernate-mapping >
< class? name ="Collection.List.Student" ?table ="studentList" ? >
????
< id? name ="id" ?unsaved-value ="null" >
??????
< generator? class ="uuid.hex" ></ generator >
????
</ id >
????
< property? name ="cardid" ?type ="string" ?column ="cardid" ></ property >
????
< property? name ="name" ?type ="string" ?column ="name" ></ property >
????
< property? name ="age" ?type ="int" ?column ="age" ></ property >
????
< property? name ="description" ?type ="string" ?column ="description" ></ property > ?
????
< many-to-one? name ="team" ?
?????????????????column
="team_id"
?????????????????class
="Collection.List.Team" ?
?????????????????cascade
="none" ?
?????????????????fetch
="join" >
????
</ many-to-one >
</ class >

</ hibernate-mapping >

?

Hibernate.cfg.xml

?

<? xml?version='1.0'?encoding='UTF-8' ?>
<! DOCTYPE?hibernate-configuration?PUBLIC
??????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
??????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<!-- ?Generated?by?MyEclipse?Hibernate?Tools.??????????????????? -->
< hibernate-configuration >

< session-factory >
????
< property? name ="connection.username" > root </ property >
????
< property? name ="connection.url" >
????????jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp; useUnicode=true
????
</ property >
????
< property? name ="dialect" >
????????org.hibernate.dialect.MySQLDialect
????
</ property >
????
< property? name ="myeclipse.connection.profile" > mysql </ property >
????
< property&n

Hibernate集合映射之List


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久综合噜噜激激的五月天 | 欧美亚洲国产另类 | 波多野结衣中文无毒不卡 | 四虎国产 | 久久一区二区精品 | 国产成人亚洲综合在线 | 成人一级黄色毛片 | 天天做天天添婷婷我也去 | 欧毛片| 亚洲美女在线视频 | 国产片一级aaa毛片视频 | 青草视频网 | 国产九九在线观看播放 | 国产福利在线永久视频 | 国产精品91视频 | 最新中文字幕一区二区乱码 | 亚洲欧洲一区二区三区在线观看 | 婷婷丁香亚洲 | 综合精品 | α毛片 | 天天操夜夜操夜夜操 | 国产国产精品人在线观看 | 福利视频免费看 | 国产高清不卡视频 | 天天摸夜夜 | 色综合久久久久久久久五月性色 | 内部片免费一区 | 老子影院午夜伦手机不四虎 | 亚洲精品乱码蜜桃久久久 | 国产精品亚洲片在线观看麻豆 | 午夜91理论片 | 久久99精品视香蕉蕉 | 免费毛片大全 | 奇米888第四色 | 久久综合久久伊人 | 天天靠天天擦天天摸 | 久久国产精品广西柳州门 | 久久影院一区二区三区 | 国产极品福利视频在线观看 | 日韩伦理亚洲欧美在线一区 | 尤物视频在线免费观看 |