使用父引用成功添加子实体后,子资源不显示在父资源下 [英] After successfully adding child entity with parent reference, child does not show under parent resource
问题描述
我有两个实体,Shelf 和 Book.一个 Shelf 可以有多个 Books(这种关系是双向的).我已经将这两个公开为 JpaRepositories.
I have two entities, Shelf and Book. A Shelf can have multiple Books (the relationship is bi-directional). I've exposed both of these as JpaRepositories.
问题来了:
- 我通过将 { "name":"sci-fi" } 发布到/shelves.(success) 来创建一个书架
- 我通过将 { "name":"mybook", "shelf":"localhost:8080/shelves/1" } 发布到/books 来为该书架创建一本书.(成功)
- 当我拿到我刚在/books/1 创建的书时,它有指向父书架的正确链接.
- 但是当我去书架/1/books 时,我得到一个空结果,{ }!
有什么我可能遗漏的想法吗?
Any ideas what I might be missing?
现在我已经通过在 beforeCreate 事件中显式地将这本书添加到书架上构建了一个解决方法,但这似乎完全没有必要.(不过,它确实解决了问题.)
Right now I've constructed a workaround by explicitly adding the book to its shelf in a beforeCreate event, but it seems like this should be totally unnecessary. (It does fix the problem, however.)
@HandleBeforeCreate(Book.class)
public void handleCreate(Book book) {
// why is this necessary?
book.getShelf().getBooks().add(book);
}
以下是实体类:
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@ManyToOne
private Shelf shelf;
public Shelf getShelf() {
return shelf;
}
public void setShelf(Shelf shelf) {
this.shelf = shelf;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
@Entity
public class Shelf {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany
private List<Book> books = new ArrayList<Book>();
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Shelf other = (Shelf) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
我使用的是 Spring Boot 1.1.8.
I'm using Spring Boot 1.1.8.
推荐答案
在你的 Shelf 实体中,将 mappedBy = "self"
属性添加到 @OneToMany
注释中书籍:
In your Shelf entity, add the mappedBy = "self"
property to the @OneToMany
annotation in books:
@OneToMany(mappedBy = "self")
private List<Book> books = new ArrayList<Book>();
这将使用与 self
的引用匹配的书籍自动填充书籍列表.
This will auto populate the list of books with the books whose reference to self
matches.
这篇关于使用父引用成功添加子实体后,子资源不显示在父资源下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!