Generated Mapping Files
As seen in Figure 4.2, the examresult table is a many-to-many join with an additional data attribute. Because of the additional data (the score value), we can't model this using Hibernate's built-in many-to-many functionality. Instead it must be modeled as a separate class.
Listing 4.9 shows the generated *.hbm.xml file for the Examresult class (the white space has been reformatted slightly to make it more readable). Note the two many-to-one associations; these are used in lieu of a Hibernate many-to-many relationship.
Listing 4.9. Generated Many-to-Many Examresult Class
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="com.cascadetg.ch04.Examresult" table="examresult">
<id name="id" type="java.lang.Long" column="ID">
<generator class="native" />
</id>
<property name="score" type="int" column="score"
length="11" />
<!-- associations >
<!-- bi-directional many-to-one association to Student -->
<many-to-one name="student"
class="com.cascadetg.ch04.Student"
not-null="true">
<column name="studentID" />
</many-to-one>
<!-- bi-directional many-to-one association to Exam -->
<many-to-one name="exam" class="com.cascadetg.ch04.Exam"
not-null="true" >
<column name="examID" />
</many-to-one>
</class>
</hibernate-mapping>
The flip side of the association with Examresult is shown in Listing 4.10 (again, the white space has been reformatted to make it more readable). The Exam mapping uses a set to manage the relationship. For more information on the set tag and the other nested portions of the mapping, see Chapter 5.
Listing 4.10. Generated Exam Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"--//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="com.cascadetg.ch04.Exam" table="exam">
<id name="id" type="java.lang.Long" column="ID" >
<generator class="native" />
</id>
<property name="date" type="java.sql.Timestamp"
column="date" length="14" />
<property name="comment" type="java.lang.String"
column="comment" length="255" />
<!-- associations -->
<!-- bi-directional many-to-one association to Course -->
<many-to-one name="course"
class="com.cascadetg.ch04.Course"
not-null="true" >
<column name="courseID" />
</many-to-one>
<!-- bi-directional one-to-many association to Examresult -->
<set name="examresults" lazy="true" inverse="true" >
<key>
<column name="examID" />
</key>
<one-to-many class="com.cascadetg.ch04.Examresult" />
</set>
</class>
</hibernate-mapping>
The mapping generated for the student table is shown in Listing 4.11 (extraneous white space removed).
Listing 4.11. Generated Student Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="com.cascadetg.ch04.Student" table="student">
<id name="id" type="java.lang.Long" column="ID" >
<generator class="native" />
</id>
<property name="firstName" type="java.lang.String"
column="firstName" length="100" />
<property name="lastName" type="java.lang.String"
column="lastName" length="100" />
<property name="idString" type="java.lang.String"
column="idString" length="20" />
<!-- associations -->
<!-- bi-directional one-to-many association to Examresult -->
<set name="examresults" lazy="true" inverse="true">
<key>
<column name="studentID" />
</key>
<one-to-many class="com.cascadetg.ch04.Examresult" />
</set>
</class>
</hibernate-mapping>
The mapping generated for the final table, course, is shown in Listing 4.12 (white space edited).
Listing 4.12. Generated Course Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"--//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="com.cascadetg.ch04.Course" table="course" >
<id name="id" type="java.lang.Long" column="ID" >
<generator class="native" />
</id>
<property name="title" type="java.lang.String"
column="title" length="100" />
<property name="quarter" type="java.lang.String"
column="quarter" length="4" />
<!-- associations -->
<!-- bi-directional one-to-many association to Exam -->
<set name="exams" lazy="true" inverse="true" >
<key>
<column name="courseID" />
</key>
<one-to-many class="com.cascadetg.ch04.Exam"/>
</set>
</class>
</hibernate-mapping>
 |