The Java Classes
For the entities student and course we create two simple Bean classes. For the complete source code check out the upper left box on this page.
The class student has the properties and the appropriate getter and setter methods
studentNumber; studentFirstName; studentLastName
The class course has the properties and the appropriate getter and setter methods
courseNumber; courseName; description
The Java class Student contains a HashSet named
courses. This set contains the courses the student has signed in.
The Java class Course contains a HashSet named
students. This set contains all the students who have signed in for this particular course.
This is what we call a m:n relationship.
A student can assign to
many courses.
A course is assigned by
many students.
The Hibernate config-Files
The hibernate.cfg.xml conatains common settings for the database connection, the SQL dialect and a few more which are not explained in detail here..
In the StudentCourse.hbm.xml we map the classes Student and Course. Lets have a closer look at this file.
The mapping file has a root tag called
<hibernate-mapping>. Within this tag the classes are mapped for the SQL database. In more complex applications every class should have it own mapping file. To make things easy we just use one for both classes.
This is the mapping for the student class. The mapping for the courses is similar.
The mapped Student class
<class name="Student" table="Student">
<id name="studentNumber" column="student_number">
<generator class="native">
</id>
<property name="studentFirstName"/>
<property name="studentName"/>
<set name="courses"
table="Course_Student"
inverse="true"
lazy="true"
cascade="save-update">
<key column="student_number"/>
<many-to-many class="Course" column="course_number"/>
</set>
</class>
<query name="students.all">From Student</query>
In the opening
<class> tag we provide the name of the class and the table of the database where the students are stored.
In the
<id> tag we need to contribute the primary key of the Class and the related column in the database. In our case the property
studentNumber is the property which represents the primary key. This key is stored in the column
student_number of the table student.
Now we declare the properties. As long the name of the property equals the name of the database column no further declarations have to be made (Hibernate offers a lot of possibilities for fine tuning here but this is not our subject here).
In the
<set> tag we declare a collection which contains the course objects for the courses the student has assigned. The name of the set in the config-file has to be the same as the name of the property in the student class. For the table we declare the table which manages the m:n relationship.
Remember: for a m:n relationship you need a help-table which contains only the primary key pairs of the related classes. The table course_student contains the two colums student_number and course_number. These columns represent the primary keys of the primary keys of the tables student and course.
With the tag
<many-to-many> we tell Hibernate the kind of the relationship, the Class at the "other side" of the realtionship and the primary key column of the "other-side" Class.
If we set the property lazy to true the Student Class will be instantiated with an empty course-set. In the cascade tag you can tell Hibernate when to fill this set.
At the end of the config-file we declare a HQL-Query. This Query is used later by the Flex application to fill a Collection with Student objects. We come back to this query further down in the Box:
The Flex application.
The mapping for the class course is more or less similar.
13. September 2007