Reputation: 419
Let's say I have to entity classes Person and Book, both having a field name date (p.e. of type java.util.Date) which is representing the birth date for Person and the publish date for Books and of course both are not unique. Now in the Entity class Person I want to have a Relation (ManyToMany)to Books, which returns all Books published on the person's birth date. Is this possible and if so how to I have to define the ManyToMany relation (and the JointTable) ? Please note that the field is called date in both entities and is not unique.
I tried
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "test",
joinColumns = @JoinColumn(name = "date", unique=false),
inverseJoinColumns = @JoinColumn(name = "date", unique=false)
)
but it results in an exception at start time ( org.hibernate.MappingException: Repeated column in mapping for collection)
and I tried
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "test",
joinColumns = @JoinColumn(name = "date_person", unique=false),
inverseJoinColumns = @JoinColumn(name = "date_book", unique=false)
)
but it result in an empty table to be created. I must be missing something.
Upvotes: 0
Views: 909
Reputation: 691655
The role of an association is not to model such kinds of relationships. ManyToMany doesn't work with arbitrary columns happening to have the same name and value. It works with join tables containing a foreign key to each of the joined entities.
To get all the books that were published on the same date as the birth date of a person, just execute a query:
select b from Book b where b.date = :dateOfBithOfPerson
Upvotes: 3