2

我有如下两张表,问题是每本书只能由一个人拥有。

Person_ID Book_ID
1           1          <<correct
2           1          << ERORR

如何改变它以使每本书可以被很多人拥有?

人.java

@Entity
public class Person {
  private long id;
  .....
  @OneToMany(cascade = CascadeType.ALL)
  public Set<Book> getBooks() {
        return books;
  }
 ....
}

图书.java

@Entity
public class Book {
 private long id;
 ....
}
4

2 回答 2

2

您需要将关系更改为 ManyToMany

于 2013-08-14T05:16:57.707 回答
1

您可以使用注释@ManyToMany

@Entity
 public class Person {
 private long id;
.....
 @ManyToMany(cascade = CascadeType.ALL)
 public Set<Book> getBooks() {
      return books;
   }
 ....
  }
于 2013-08-14T05:29:06.277 回答