java - 实体间的优化关系

标签 java spring jpa spring-data

我有这样的实体:

@Entity
@Table(name = "test")
public class TestEntity {

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes = new HashSet<>();

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type1", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes1 = new HashSet<>();

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type2", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes2 = new HashSet<>();
}

我在数据库中有 1000 条实体记录。当我尝试获取 1000 个实体时,我在日志中看到:

  • 1000 select到实体表
  • 1000 根据test_entity_id 选择到test_entity_record_type 表
  • 1000 根据test_entity_id 选择到test_entity_record_type1 表
  • 1000 根据test_entity_id 选择到test_entity_record_type2 表

结果回答时间很长。

如何缩短响应时间?

最佳答案

您可以使用@BatchSize 注解:

@BatchSize(size = 1000)
private Set<RecordType> recordTypes = new HashSet<>();

关于java - 实体间的优化关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65303175/

相关文章:

java - 如何将图像存储到 Oracle 数据库?

java - Spring mvc : controller returns [][], 可在 jsp 中使用 foreach,但如何绑定(bind)?

java - 防止 hibernate 实体更改被持久化

java - 如何使用继承处理 JPA CollectionTable?

java - 通过 Ajax 请求的 Servlet 流

java - 井字棋游戏逻辑

java - 将对象数据从 JSP 传递到 Spring Controller Post 方法而不是 GET 方法

java - Spring MVC 为匹配模式的所有 url 提供相同的静态内容

java - jpa中@ColumnDefault的值字段是什么?

java - 双向 @OneToMany 映射对象(父级和子级): parent gets saved, 子级不