Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate many to many with a composite key with one field shared on either side of the relationship
    primarykey
    data
    text
    <p>This is a conceptional example, but I do not understand how to implement this.</p> <p>I have tables as follows:</p> <pre><code>BOOK ------------ BOOK_ID BOOK_AUTHOR ------------ BOOK_ID AUTHOR_ID AUTHOR ------------ AUTHOR_ID </code></pre> <p>Which map to the following hibernate many to many relationship:</p> <p>Book</p> <pre><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; } } </code></pre> <p>Author</p> <pre><code>@Entity public class Author implements Serializable { @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "authors", targetEntity = Book.class ) public Collection getBooks() { return books; } </code></pre> <p>} </p> <p>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:</p> <pre><code>BOOK ------------ BOOK_ID PUBLISHER_ID BOOK_AUTHOR ------------ BOOK_ID AUTHOR_ID PUBLISHER_ID AUTHOR ------------ AUTHOR_ID PUBLISHER_ID </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload