【发布时间】:2017-12-12 19:11:46
【问题描述】:
我正在使用 Spring Data JPA 和 H2 数据库开发 Spring Boot 应用程序。我正在使用 spring-data-jpa。 当我使用 ManyToMany 映射器类来获取另一个类的数据时。但我发现它是NULL。
书籍类
@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "BOOK_AUTHOR", joinColumns = {
@JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")}, inverseJoinColumns = {
@JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID")})
private Set<Author> authors;
public Book() {
super();
}
public Book(String name) {
super();
this.name = name;
this.authors = new HashSet<>();
}
public Book(String name, Set<Author> authors) {
super();
this.name = name;
this.authors = authors;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
@Override
public String toString() {
return String.format("Book [id=%s, name=%s, authors=%s]", id, name, authors);
}
}
作者.class
@Entity
public class Author implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(mappedBy = "authors")
private Set<Book> books;
//why CAN NOT GET the data when using these code else ?
// @ManyToMany(cascade = CascadeType.ALL)
// @JoinTable(name = "BOOK_AUTHOR", joinColumns = {
// @JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID")},
//inverseJoinColumns = {
// @JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")})
// private Set<Book> books;
public Author() {
super();
}
public Author(String name) {
super();
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
@Override
public String toString() {
return String.format("Author [id=%s, name=%s, books=%s]", id, name, books);
}
}
在 test.class 中测试代码快照器
List<Book> books = bookRepository.findAll();
for (Book it : books) {
Set<Author> authors = it.getAuthors();
//CAN get authors data.
System.out.println(authors.size());
}
assertThat(bookRepository.findAll()).hasSize(2);
List<Author> authors = authorRepository.findAll();
for (Author it : authors) {
//CAN NOT get books data ? Why and HOW ?
//HOW can I get the books data ? Or other ways ?
// thanks
Set<Book> books1 = it.getBooks();
assertThat(books1 == null);
}
assertThat(authorRepository.findAll()).hasSize(3);
我的代码有错误吗? 还是其他方式?
感谢有很大不同。
【问题讨论】:
-
Author对象在访问其getBooks方法时处于什么生命周期状态?在此之前是否加载了数据? JPA 提供者有一个日志告诉你这些事情。数据库中的数据是否正确? -
你的代码应该可以工作,除非你刚刚在同一个事务中创建了书籍和作者,而忘记在作者中设置书籍。 JPA 从不将集合设置为 null,您也不应该这样做。将您的集合初始化为空集合。
标签: java spring hibernate spring-mvc spring-data-jpa