Problem too specific.closing the same.
views:
71answers:
1
+2
A:
It is possible to map a ManyToMany
association using a Map
, and a typicall use case for this kind of mapping is when you have a ternary association. For example, if you have:
STUDENT_TEACHER_COURSE STUDENT_ID(FK, PK) TEACHER_ID(FK, PK) COURSE_ID (FK, PK)
Then you could define the following mapping (assuming you're using a Hibernate Annotations < 3.5):
@Entity
public class Student {
...
@ManyToMany
@JoinTable(
name="STUDENT_TEACHER_COURSE",
joinColumns= { @JoinColumn(name="STUDENT_ID") },
inverseJoinColumns= { @JoinColumn(name="TEACHER_ID") }
)
@MapKeyManyToMany(joinColumns = @JoinColumn(name="COURSE_ID",unique = false))
protected Map<Course,Teacher> teachers ;
...
}
But I don't think you can have a "nested" List<Teacher>
as value inside the Map
, I don't think Hibernate can map that and I'd consider getting the List
from an entity instead.
References
- Hibernate Annotations 3.4 Reference Guide
- Hibernate Annotations 3.5 Reference Guide
Resources
Pascal Thivent
2010-10-02 20:19:08
@Arthur Thanks. Do you see/know any better approach? What the OP wants looks very exotic to me.
Pascal Thivent
2010-10-03 00:49:41
@Pascal Thivent Your solution sounds good. But if he want an exotic mapping, he should use a lot of wrappers @Embeddable classes. I hope he provide some information about your approach.
Arthur Ronald F D Garcia
2010-10-03 03:03:39
@Pascal Thivent When i said *A lot of relationships* i refer to the mapping provided by @Maddy.Shik :)
Arthur Ronald F D Garcia
2010-10-03 03:10:40
@Pascal Thivent Thanks for solution. As u suggested approach i am following is little exotic and nested collection is also not possible with hibernate. You mentioned to use Entity instead of list, that is possible only if i use Class corresponding to mapping table which doesn't seem in accordance with coding principles. Can u please suggest your approach?
Maddy.Shik
2010-10-03 05:49:00