M: N ratio in JPA (table data wrapping)

I have an m: n relationship book - borrowing - user , borrowing is a table of connections.

The tables are given (cannot be changed):

  • on the one hand, they are also used by the jdbc application.
  • on the other hand I would like to use them through jpa

book (book_id) - borrow (book_id, used_id) - user (user_id)

 used jpa annotations:
 User:
 @OneToMany(targetEntity=BorrowEntity.class, mappedBy="user")
 @JoinColumn(name="USER_ID", referencedColumnName="USER_ID")    
 private List<BorrowEntity>borrowings;

 Book: 
 @OneToMany(targetEntity=BorrowEntity.class, mappedBy="book")
 @JoinColumn(name="BOOK_ID", referencedColumnName="BOOK_ID")
 private List<BorrowEntity>borrowings;

My problem is that in the settings above it adds some additional (unwanted) fields to the loan table:

'user_USER_ID' and 'book_BOOK_ID'

How can I configure jpa annotations to save only the "Borrowing": user_id, book_id, which is quite a lot in one?

Take a look at the picture that shows more:

book borrowing

+5
1

, , . , , ManyToMany, JoinTable.

@ManyToMany
@JoinTable(name = "borrow",
           joinColumns = @JoinColumn(name = "USER_ID"),
           inverseJoinColumns = @JoinColumn(name = "BOOK_ID"))
private List<Book> borrowedBooks;
...

@ManyToMany(mappedBy = "borrowedBooks")
private List<User> borrowingUsers;

, ManyToOne ( ). , :

@OneToMany(targetEntity=BorrowEntity.class, mappedBy="user")
@JoinColumn(name="USER_ID", referencedColumnName="USER_ID")    
private List<BorrowEntity>borrowings;

, mappedBy : OneToMany/ManyToOne, user BorrowEntity. , , , .

, @JoinColumn . mappedBy. :

@OneToMany(mappedBy="user")
private List<BorrowEntity>borrowings;

TargetEntity , List<BorrowEntity>: JPA .

+7

All Articles