Spring 引导获取相关实体的空 _embedded 数组

Spring Boot getting empty _embedded array for related entity

使用 Spring 引导我有以下实体的缩写结构:

@Entity
@Table(name = "item")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false)
    protected Long id;
...
}
@Entity
@Table(name = "book")
public class Book extends Item implements Serializable {
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "item_author", joinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
    private Set<Author> authors;
}
@Entity
@Table(name = "author")
public class Author implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();

    private String name;
}

我的 DAO 只是所有实体的简单 RestResource 接口,例如:

@RestResource(path="items", rel="items")
  public interface ItemDao extends CrudRepository<Item, Long> {
}

当我通过 id 查询实体时,一切都很好

GET > http://localhost:8080/shelfventory/authors/1
{
  "name" : "Jhonny Cash",
  "used" : true,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/shelfventory/authors/1"
    },
    "author" : {
      "href" : "http://localhost:8080/shelfventory/authors/1"
    },
    "books" : {
      "href" : "http://localhost:8080/shelfventory/authors/1/books"
    }
  }
}

但是当我尝试点击相关对象的链接时,我只得到一个空的嵌入式:

GET > http://localhost:8080/shelfventory/authors/1/books
{
  "_embedded" : {
    "books" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/shelfventory/authors/1/books"
    }
  }
}

我做错了什么,如何解决?

考虑将这两个属性添加到您的 application.properties 以保持您的 @Entity 和架构同步:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=true