Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, since the borrow table is a pure join table, you don't need to map it at all. All you need is a ManyToMany association using this borrow table as JoinTable.</p> <pre><code>@ManyToMany @JoinTable(name = "borrow", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "BOOK_ID")) private List&lt;Book&gt; borrowedBooks; ... @ManyToMany(mappedBy = "borrowedBooks") private List&lt;User&gt; borrowingUsers; </code></pre> <p>If you really want to map the join table as an entity, then it should contain two ManyToOne associations (one for each foreign key). So the following is wrong:</p> <pre><code>@OneToMany(targetEntity=BorrowEntity.class, mappedBy="user") @JoinColumn(name="USER_ID", referencedColumnName="USER_ID") private List&lt;BorrowEntity&gt;borrowings; </code></pre> <p>Indeed, <code>mappedBy</code> means: this association is the inverse side of the bidirectional OneToMany/ManyToOne association, which is already mapped by the field <code>user</code> in the <code>BorrowEntity</code> entity. Please see the annotations on this field to know how to map the association.</p> <p>So the <code>@JoinColumn</code> doesn't make sense. It's in contradiction with <code>mappedBy</code>. You just need the following:</p> <pre><code>@OneToMany(mappedBy="user") private List&lt;BorrowEntity&gt;borrowings; </code></pre> <p>The targetEntity is also superfluous, since it's a <code>List&lt;BorrowEntity&gt;</code>: JPA can infer the target entity from the generic type of the list.</p>
 

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