JPA-另一个表中使用的多对多主键

时间:2018-08-10 16:30:38

标签: java jpa relational-database entity persistence

我希望能够在我的个人实体中拥有书籍列表以及每本书的当前页面。 (给定的书籍收藏可以供多人使用)

我有下表:

BookCollection
---------------
collection_id
collection_name


Book
---------------
book_id
book_name


CollectionBooks
---------------
cb_id
collection_id
book_id


Person
---------------
person_id
person_name


PersonBooks
---------------
pb_id
person_id
cb_id
current_page

前四个表已建立,并且关系正常工作。我对如何将JPA用于PersonBooks表感到困惑。我相信,这是数据库中确定一个人拥有的书以及该书中的书页所需要的全部内容。

这是实体类

@Entity
public class BookCollection {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long collectionId;

    private String collectionName;

    @ManyToMany
    @JoinTable(name="CollectionBooks", 
               joinColumns=@JoinColumn(name="collection_id", referencedColumnName="collectionId"), 
               inverseJoinColumns=@JoinColumn(name="book_id", referencedColumnName="bookId"))
    private Set<Book> books;
}

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long bookId;

    private String bookName;
}

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long personId;

    private String personName;
}

0 个答案:

没有答案