many-to-many

第一种

就是定义两个实体对应数据库中的两张表,并且再定义一个中间实体对应数据库中的第三张表,用于维护两个实体间的多对多关联关系

1.Student.java

Java代码

packagecom.template.model;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-20

*Time:下午9:31

*TochangethistemplateuseFile|Settings|FileTemplates.

*/

publicclassStudent{

privateIntegerid;

privateStringname;

privateList<Borrowed>borroweds;

publicStudent(){

}

}

2.Student.hbm.xml

Xml代码

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.Student"table="student">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="name"column="name"type="java.lang.String"/>

<bagname="borroweds"table="borrowed">

<keycolumn="studentid"/>

<one-to-manyclass="com.template.model.Borrowed"/>

</bag>

</class>

</hibernate-mapping>

3.Book.java

Java代码

packagecom.template.model;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-20

*Time:下午9:31

*TochangethistemplateuseFile|Settings|FileTemplates.

*/

publicclassBook{

privateIntegerid;

privateStringname;

privateList<Borrowed>borroweds;

publicBook(){

}

}

4.Book.hbm.xml

Xml代码

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.Book"table="book">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="name"column="name"type="java.lang.String"/>

<bagname="borroweds"table="borrowed">

<keycolumn="bookid"/>

<one-to-manyclass="com.template.model.Borrowed"/>

</bag>

</class>

</hibernate-mapping>

5.Borrowed.java

Java代码

packagecom.template.model;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-20

*Time:下午9:31

*TochangethistemplateuseFile|Settings|FileTemplates.

*/

publicclassBorrowed{

privateIntegerid;

privateStudentstudent;

privateBookbook;

publicBorrowed(){

}

}

6.Borrowed.hbm.xml

Xml代码

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.Borrowed"table="borrowed">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<many-to-onename="book"class="com.template.model.Book"column="bookid"/>

<many-to-onename="student"class="com.template.model.Student"column="studentid"/>

</class>

</hibernate-mapping>

第二种

就是只定义两个实体对应数据库中的两张表,不需要定义第三个实体,而是在这两个实体中定义多对多的集合属性来维护这两个实体间的多对多关联关系

一。Concert.java

Java代码

packagecom.template.model.many;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-30

*Time:下午4:56

*/

publicclassConcert{

privateIntegerid;

privateStringname;

privateStringsinger;

privateList<Venue>venues;

publicConcert(){

}

}

二。Concern.hbm.xml

Xml代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.many.Concert"table="concern"dynamic-insert="true"dynamic-update="true">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="name"column="name"type="java.lang.String"/>

<propertyname="singer"column="singer"type="java.lang.String"/>

<bagname="venues"table="concern_venue">

<keycolumn="concernid"/>

<many-to-manyclass="com.template.model.many.Venue"column="venueid"/>

</bag>

</class>

</hibernate-mapping>

三。Venue.java

Java代码

packagecom.template.model.many;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-30

*Time:下午4:56

*/

publicclassVenue{

privateIntegerid;

privateStringaddress;

privateList<Concert>concerts;

publicVenue(){

}

}

四。Venue.hbm.xml

Xml代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.many.Venue"table="venue"dynamic-insert="true"dynamic-update="true">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="address"column="address"type="java.lang.String"/>

<bagname="concerts"table="concern_venue">

<keycolumn="venueid"/>

<many-to-manyclass="com.template.model.many.Concern"column="concernid"/>

</bag>

</class>

</hibernate-mapping>

看看数据库中的表结构吧

在第一种方法中,为中间实体也定义了一个Hibernate实体映射文件,使用的是one-to-many元素,而在第二种方法中则使用的是many-to-many元素。

一。Concert.java

Java代码

packagecom.template.model.many;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-30

*Time:下午4:56

*/

publicclassConcert{

privateIntegerid;

privateStringname;

privateStringsinger;

privateList<Venue>venues;

publicConcert(){

}

}

二。Concern.hbm.xml

Xml代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.many.Concert"table="concern"dynamic-insert="true"dynamic-update="true">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="name"column="name"type="java.lang.String"/>

<propertyname="singer"column="singer"type="java.lang.String"/>

<bagname="venues"table="concern_venue">

<keycolumn="concernid"/>

<many-to-manyclass="com.template.model.many.Venue"column="venueid"/>

</bag>

</class>

</hibernate-mapping>

三。Venue.java

Java代码

packagecom.template.model.many;

importjava.util.List;

/**

*CreatedbyIntelliJIDEA.

*User:ZhongGang

*Date:11-7-30

*Time:下午4:56

*/

publicclassVenue{

privateIntegerid;

privateStringaddress;

privateList<Concert>concerts;

publicVenue(){

}

}

四。Venue.hbm.xml

Xml代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingdefault-access="field">

<classname="com.template.model.many.Venue"table="venue"dynamic-insert="true"dynamic-update="true">

<idname="id"column="id"type="java.lang.Integer">

<generatorclass="native"/>

</id>

<propertyname="address"column="address"type="java.lang.String"/>

<bagname="concerts"table="concern_venue">

<keycolumn="venueid"/>

<many-to-manyclass="com.template.model.many.Concern"column="concernid"/>

</bag>

</class>

</hibernate-mapping>

看看数据库中的表结构吧

在第一种方法中,为中间实体也定义了一个Hibernate实体映射文件,使用的是one-to-many元素,而在第二种方法中则使用的是many-to-many元素。

相关推荐