This is a conceptional example, but I do not understand how to implement this.
I have tables as follows:
Code:
BOOK
------------
BOOK_ID
BOOK_AUTHOR
------------
BOOK_ID
AUTHOR_ID
AUTHOR
------------
AUTHOR_ID
Which map to the following hibernate many to many relationship:
Book
Code:
@Entity
public class Book implements Serializable {
@ManyToMany(
targetEntity=org.hibernate.test.metadata.manytomany.Author.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="BOOK_AUTHOR",
joinColumns=@JoinColumn(name="BOOK_ID"),
inverseJoinColumns=@JoinColumn(name="AUTHOR_ID")
)
public Collection getAuthors() {
return authors;
}
}
Author
Code:
@Entity
public class Author implements Serializable {
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "authors",
targetEntity = Book.class
)
public Collection getBooks() {
return books;
}
}
However, I live in a world where, a book and an author must have matching publishers but a book can exist without an author and an author can exist without a book. So, to my tables I have added a publisher id:
Code:
BOOK
------------
BOOK_ID
PUBLISHER_ID
BOOK_AUTHOR
------------
BOOK_ID
AUTHOR_ID
PUBLISHER_ID
AUTHOR
------------
AUTHOR_ID
PUBLISHER_ID
This in the database layer, would mean that the publisher id must match across all three tables meaning a book and author must have the same publisher, but how do I implement this using hibernate mapping? This is
not a question on how to map things if I have an additional field such as description in the join table. It is how to map things so the publisher id can be forced to match across all three tables when creating a book_author relationship.