Tuesday, October 18, 2011

Many to Many unidirectional association

Mapping using Hibernate XML mapping:

Let us go the package com.demo.manyToManyUnidirectional and execute class ManyToManyUnidirectionalExecutor.java.

Things to look into:
* The ORM classes Student.java and Course.java
* The xml mapping file manyToManyUnidirectional.hbm.xml

ORM:


public class Student {

private long id;

private String studentName;

private Set courses = new HashSet();

//getter and setter methods
}

public class Course {

private long id;

private String courseName;

//getter and setter methods
}

* Note that only Student entity has the references to Course entity and the later entity does not have any reference to the former entity. Hence only a unidirectional navigation is possible.

XML:
<hibernate-mapping auto-import="false">

<class name="com.demo.manyToManyUnidirectional.Student" table="STUDENTS" lazy="false">
<id name="id" column="STUDENT_ID"> <generator class="native" /> </id>
<property name="studentName" column="STUDENT_NAME" />
<set name="courses" table="STUDENT_COURSE" lazy="false" cascade="save-update">
<
key column="STUDENT_ID" not-null="true" />
<
many-to-many class="com.demo.manyToManyUnidirectional.Course" column="COURSE_ID" />
</set>
</class>

<class name="com.demo.manyToManyUnidirectional.Course" table="COURSES" lazy="false">
<id name="id" column="COURSE_ID">
<generator class="native" /> </id>
<property name="courseName" column="COURSE_NAME" />
</class>

</hibernate-mapping>


* <set> refers the to-many association and also declares the collection table.
* cascase declares that inserting the objects will be taken care by the Student entity.
* <key> refers the foreign key column of the collection table.

* <many-to-many> declares the many-to-many association.


Once the ManyToManyUnidirectionalExecutor.java is executed, verify the results in the HSQL DB manager by executing the simple select queries on the tables STUDENTS, COURSES and STUDENT_COURSE as follows.





Mapped by Annotations:

Let us go to the package annotation.com.demo.manyToManyUnidirectional and execute the class ManyToManyAnnotationUniDirectionalExecutor.java

Things to look into:

The annotated ORM classes Student.java and Course.java

ORM:

@Entity(name="annotation.com.demo.manyToManyUnidirectional.Student")
@Table(name="STUDENTS")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="STUDENT_ID")
private long id;

@Column(name="STUDENT_NAME")
private String studentName;

@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name="STUDENT_COURSE",joinColumns=@JoinColumn(name="STUDENT_ID"),inverseJoinColumns=@JoinColumn(name="COURSE_ID"))
private Set courses = new HashSet();

//getter and setter methods
}

@Entity(name="annotation.com.demo.manyToManyUnidirectional.Course")
@Table(name="COURSES")
public class Course {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="COURSE_ID")
private long id;

@Column(name="COURSE_NAME")
private String courseName;

//getter and setter methods
}

*
@ManyToMany declares the many-to-many association
*
cascase declares that inserting the objects will be taken care by the Student entity.
* @JoinTable refers the table where the collection values will be stored.
* @JoinColumn represents the foreign key column of the collection table.

Once the
ManyToManyAnnotationUniDirectionalExecutor.java is executed , verify the results as mentioned above.