日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Hibernate關(guān)聯(lián)學(xué)習(xí)之---多對多關(guān)聯(lián)

系統(tǒng) 1684 0

有兩個實Student和Course,他們之間的關(guān)系是多對多,在數(shù)據(jù)庫上,為了滿足3NF,會建立一個中間表Stu_Course來維護Student和Course之間的關(guān)系,在Hibernate中,則使用雙向多對多來體現(xiàn)這種結(jié)構(gòu)

數(shù)據(jù)庫腳本

?

create ? table ?stu(id? varchar ( 32 )? primary ? key ,name? varchar ( 32 ),cardid? varchar ( 32 ),age? int );
create ? table ?course(id? varchar ( 32 )? primary ? key ,name? varchar ( 32 ));
create ? table ?stu_course(stu_id? varchar ( 32 ),course_id? varchar ( 32 ));



insert ? into ?stu? values (" 1 ","tom"," 200701 ", 11 );
insert ? into ?stu? values (" 2 ","tomclus"," 200702 ", 11 );
insert ? into ?stu? values (" 3 ","spark"," 200703 ", 11 );
insert ? into ?stu? values (" 4 ","jerry"," 200704 ", 11 );

insert ? into ?course? values (" 1 ","history");
insert ? into ?course? values (" 2 ","computer");
insert ? into ?course? values (" 3 ","music");
insert ? into ?course? values (" 4 ","encomic");
insert ? into ?course? values (" 5 ","politics");

insert ? into ?stu_course? values (" 2 "," 1 ");
insert ? into ?stu_course? values (" 2 "," 2 ");
insert ? into ?stu_course? values (" 2 "," 3 ");
insert ? into ?stu_course? values (" 1 "," 3 ");
insert ? into ?stu_course? values (" 1 "," 4 ");
insert ? into ?stu_course? values (" 3 "," 2 ");
insert ? into ?stu_course? values (" 3 "," 3 ");

?POJO:

?

package ?Relation.ManytoMany;

import ?java.util.Set;


public ? class ?Stu? ... {
??
private ?String?id;
??
private ?String?cardid;
??
private ?String?name;
??
private ? int ?age;
??
private ?Set?course;
public ?Set?getCourse()? ... {
????
return ?course;
}

public ? void ?setCourse(Set?course)? ... {
????
this .course? = ?course;
}

public ?String?getId()? ... {
????
return ?id;
}

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

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

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

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

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

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

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



}




package ?Relation.ManytoMany;


import ?java.util.HashSet;
import ?java.util.Set;

public ? class ?Course? ... {
???
private ?String?id;
???
private ?Set?students;
???
private ?String?name;
public ?String?getId()? ... {
????
return ?id;
}

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


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

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

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

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




}

?

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? name ="connection.password" > 1234 </ property >
????
< property? name ="connection.driver_class" >
????????com.mysql.jdbc.Driver
????
</ property >
????
< property? name ="hibernate.dialect" >
????????org.hibernate.dialect.MySQLDialect
????
</ property >
????
< property? name ="hibernate.show_sql" > true </ property >
????
< property? name ="current_session_context_class" > thread </ property >
????
< property? name ="jdbc.batch_size" > 15 </ property >
????
< mapping? resource ="Relation/ManytoMany/Course.hbm.xml" ? />
????
< mapping? resource ="Relation/ManytoMany/Stu.hbm.xml" ? />



</ session-factory >

</ hibernate-configuration >

?

Stu.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 ="Relation.ManytoMany.Stu" ?table ="stu" >
??
< id? name ="id" ?unsaved-value ="null" >
????
< generator? class ="uuid.hex" ></ generator >
??
</ id >
??
< property? name ="cardid" ?type ="string" />
??
< property? name ="name" ?type ="string" />
??
< property? name ="age" ?type ="int" />
&nb

Hibernate關(guān)聯(lián)學(xué)習(xí)之---多對多關(guān)聯(lián)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 鄂托克前旗| 迁安市| 巴彦县| 工布江达县| 甘南县| 报价| 敖汉旗| 德令哈市| 九龙坡区| 淮滨县| 兴宁市| 全椒县| 泗洪县| 定西市| 长沙市| 镇宁| 牡丹江市| 双辽市| 大英县| 新巴尔虎左旗| 长乐市| 铜川市| 图们市| 五指山市| 萝北县| 阿荣旗| 闻喜县| 章丘市| 泰宁县| 张家港市| 丰镇市| 辉南县| 清徐县| 龙井市| 南郑县| 马山县| 萝北县| 华宁县| 临沂市| 任丘市| 太保市|