dehydratedpaani wrote:
So can any experienced hibernate gurus enlighten us on the problems they faced when manually managing a many-to-many relationship as two one-to-many relationships? In terms of search criteria, caching, or other areas? I dont like this, to have to manually manage the mapping.
I had experience with one system where the many-to-many relationship, mapped as an in-between child entity, was actually a one-to-many relationship; furthermore, there was some glitch in the application that caused spurious extra rows (that is, child entities) to be created. For our report queries, we ended up including a subselect:
Code:
...
LEFT OUTER JOIN (SELECT one_id,many_id FROM relationship_table
GROUP BY one_id,many_id) rel ON one_table.id=rel.one_id
LEFT OUTER JOIN many_table ON rel.many_id=many.id
...
There are other options besides a child entity and one or two <set> mappings:
* <map> with a component as the key
* use a where=" ... " attribute on the mapping. So say the
student_course table is defined as
Code:
student_id INT,
course_id INT,
PRIMARY KEY(student_id,course_id),
status CHAR(9) not null default 'ENROLLED',
check (status IN ('ENROLLED','WITHDRAWN'))
you should be able to add a mapping
Code:
<set name="enrolledCourses" table="student_course" where="status='ENROLLED'" >
<key column="student_id"/>
<many-to-many column="course_id"
unique="true"
class="Course"/>
</set>