1
嗨,大家好,我剛剛開始學習春季啓動,想知道如何通過表單提交保存多對多關係的對象?春季引導+ jpa,多對多協會通過表單提交
說我們的書和出版商
@Entity
public class Book{
private long id;
private String name;
private List<Publisher> publishers;
public Book() {
}
public Book(String name) {
this.name = name;
}
public Book(String name, Set<Publisher> publishers){
this.name = name;
this.publishers = publishers;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "book_publisher", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "publisher_id", referencedColumnName = "id"))
public List<Publisher> getPublishers() {
return publishers;
}
public void setPublishers(List<Publisher> publishers) {
this.publishers = publishers;
}
}
@Entity
public class Publisher {
private Long id;
private String name;
private List<Book> books;
public Publisher(){
}
public Publisher(String name){
this.name = name;
}
public Publisher(String name, List<Book> books){
this.name = name;
this.books = books;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(mappedBy = "publishers")
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
的兩個實體,然後我們有一本書倉庫
public interface BookRepository extends CRUDRepository<Book, Long>{
}
會怎麼CRUD方法看起來像BookController的?
非常感謝你的回答。@ NiNiCkNaMe。 「連接表是否有附加數據」這個問題的答案是否定的。所以我需要爲出版商製作一張表格,然後以書籍形式出版商名單? – pouya