我有两张表如下,问题是每本书只能由一个人拥有。
Person_ID Book_ID
1 1 <<correct
2 1 << ERORR
如何以每本书可以被许多人拥有的方式进行更改?
Person.java
@Entity
public class Person {
private long id;
.....
@OneToMany(cascade = CascadeType.ALL)
public Set<Book> getBooks() {
return books;
}
....
}
Book.java
@Entity
public class Book {
private long id;
....
}
答案 0 :(得分:2)
您需要将关系更改为ManyToMany
答案 1 :(得分:1)
您可以使用注释@ManyToMany
@Entity
public class Person {
private long id;
.....
@ManyToMany(cascade = CascadeType.ALL)
public Set<Book> getBooks() {
return books;
}
....
}