Spring Data Rest 添加摘录投影关闭延迟获取
Spring Data Rest adding excerpt projection switches off lazy fetching
我是 Spring Data Rest 的新手,正在尝试了解它的基本概念。到目前为止一切正常,但几天前我注意到在将预测投入业务后应用程序性能突然下降。
这些是我的实体、存储库和投影
@Entity
public class Item {
@Id
@GeneratedValue(strategy = TABLE)
private long id;
private String code;
private String name;
@ManyToOne(targetEntity=Category.class)
@JoinColumn(name="category_id", referencedColumnName="id")
private Category category;
//getters & setters
}
@Entity
public class Category {
@Id
@GeneratedValue(strategy = TABLE)
private long id;
private String name;
@OneToMany(mappedBy="category", targetEntity=Item.class, fetch=FetchType.LAZY)
private Set<Item> items;
//getters & setters
}
@RepositoryRestResource(excerptProjection=ItemExcerpt.class)
public interface ItemRepository extends CrudRepository<Item, Long>{
}
@RepositoryRestResource
public interface CategoryRepository extends CrudRepository<Category, Long>{
}
@Projection(name="excerpt", types=Item.class)
public interface ItemExcerpt {
String getName();
}
所以,在我将摘录投影添加到 ItemRepository 之前,一切正常 @RepositoryRestResource(excerptProjection=ItemExcerpt.class)
在执行此操作之前,当我点击 http://localhost:9191/categories 时,Hibernate 输出如我所料:
select
category0_.id as id1_0_,
category0_.name as name2_0_
from
category category0_
这是我添加 excerptProjection=ItemExcerpt.class
后得到的输出
Hibernate:
select
category0_.id as id1_0_,
category0_.name as name2_0_
from
category category0_
Hibernate:
select
items0_.category_id as category4_1_0_,
items0_.id as id1_1_0_,
items0_.id as id1_1_1_,
items0_.category_id as category4_1_1_,
items0_.code as code2_1_1_,
items0_.name as name3_1_1_
from
item items0_
where
items0_.category_id=?
我的结论是摘录投影使得延迟获取在@OneToMany 关系上被忽略,从而导致性能下降。
有谁知道绕过这个问题的方法,或者这可能是预期的行为?
摘录投影并不能完全忽略延迟获取。更具体地说,摘录投影告诉 spring 数据在收集资源返回的任何地方包含摘录数据。
来自参考文档 Projections Excerpts、"An excerpt is a projection that is applied to a resource collection automatically."。不幸的副作用是 spring-hateoas 然后忽略 属性 而是将超媒体 link 放入资源中。我发现没有任何注释组合可以在保留输出的同时为您纠正此行为。 @JsonIgnore 不会阻止额外的查询。 @RestResource(exported = false) 会阻止查询,但也会阻止超媒体 link.
我是 Spring Data Rest 的新手,正在尝试了解它的基本概念。到目前为止一切正常,但几天前我注意到在将预测投入业务后应用程序性能突然下降。
这些是我的实体、存储库和投影
@Entity
public class Item {
@Id
@GeneratedValue(strategy = TABLE)
private long id;
private String code;
private String name;
@ManyToOne(targetEntity=Category.class)
@JoinColumn(name="category_id", referencedColumnName="id")
private Category category;
//getters & setters
}
@Entity
public class Category {
@Id
@GeneratedValue(strategy = TABLE)
private long id;
private String name;
@OneToMany(mappedBy="category", targetEntity=Item.class, fetch=FetchType.LAZY)
private Set<Item> items;
//getters & setters
}
@RepositoryRestResource(excerptProjection=ItemExcerpt.class)
public interface ItemRepository extends CrudRepository<Item, Long>{
}
@RepositoryRestResource
public interface CategoryRepository extends CrudRepository<Category, Long>{
}
@Projection(name="excerpt", types=Item.class)
public interface ItemExcerpt {
String getName();
}
所以,在我将摘录投影添加到 ItemRepository 之前,一切正常 @RepositoryRestResource(excerptProjection=ItemExcerpt.class)
在执行此操作之前,当我点击 http://localhost:9191/categories 时,Hibernate 输出如我所料:
select
category0_.id as id1_0_,
category0_.name as name2_0_
from
category category0_
这是我添加 excerptProjection=ItemExcerpt.class
Hibernate:
select
category0_.id as id1_0_,
category0_.name as name2_0_
from
category category0_
Hibernate:
select
items0_.category_id as category4_1_0_,
items0_.id as id1_1_0_,
items0_.id as id1_1_1_,
items0_.category_id as category4_1_1_,
items0_.code as code2_1_1_,
items0_.name as name3_1_1_
from
item items0_
where
items0_.category_id=?
我的结论是摘录投影使得延迟获取在@OneToMany 关系上被忽略,从而导致性能下降。 有谁知道绕过这个问题的方法,或者这可能是预期的行为?
摘录投影并不能完全忽略延迟获取。更具体地说,摘录投影告诉 spring 数据在收集资源返回的任何地方包含摘录数据。 来自参考文档 Projections Excerpts、"An excerpt is a projection that is applied to a resource collection automatically."。不幸的副作用是 spring-hateoas 然后忽略 属性 而是将超媒体 link 放入资源中。我发现没有任何注释组合可以在保留输出的同时为您纠正此行为。 @JsonIgnore 不会阻止额外的查询。 @RestResource(exported = false) 会阻止查询,但也会阻止超媒体 link.