Java 实体间的优化关系

Java 实体间的优化关系,java,spring,jpa,spring-data,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 =

我有这样的想法:

@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<>();
}
@实体
@表(name=“test”)
公共类测试{
@元素集合
@列(name=“record\u type\u id”,null=false)
@CollectionTable(name=“test\u entity\u record\u type”,joinColumns=@JoinColumn(name=“test\u entity\u id”))
private Set recordTypes=new HashSet();
@元素集合
@列(name=“record\u type\u id”,null=false)
@CollectionTable(name=“test\u entity\u record\u type1”,joinColumns=@JoinColumn(name=“test\u entity\u id”))
私有集recordTypes1=新哈希集();
@元素集合
@列(name=“record\u type\u id”,null=false)
@CollectionTable(name=“test\u entity\u record\u type2”,joinColumns=@JoinColumn(name=“test\u entity\u id”))
私有集recordTypes2=新哈希集();
}
我在数据库中有1000条实体记录。当我尝试获取1000个实体时,我在日志中看到:

  • 1000选择实体表
  • 1000选择按测试实体id测试实体记录类型表
  • 1000选择按测试实体id测试实体记录类型1表
  • 1000选择按测试实体id测试实体记录类型2表
所以回答的时间很长


如何缩短响应时间?

您可以使用@BatchSize注释:

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

我重新编写了相同的测试用例,以下配置是否比第一个配置(属性文件)的时间更短:


希望这会有所帮助。

我认为如果您正在寻找惰性模式,您应该在ElementCollection注释上使用惰性模式:@ElementCollection(fetch=FetchType.lazy)mode@OmarAmaoun作为响应,我需要查看所有字段。ElementCollections比实体关联映射更难优化,因为它们没有自己的生命周期和ID。您甚至不能通过JPQL查询它们。您确定不想切换到实体集合映射吗?@Przemek它有什么帮助吗?是的,正如@Przemek所说,jpql请求将在数据库中执行一次,而对于spring数据,它的请求数量将与数据之间的关系相同。但要停留在春季数据中,请看答案
spring.jpa.open-in-view=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=false
spring.jpa.properties.hibernate.cache.use_query_cache=false
spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.jdbc.batch_size=1000
spring.jpa.properties.hibernate.jdbc.fetch_size=1000
spring.jpa.properties.hibernate.order_inserts=true
spring.datasource.maxActive=5
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true
spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true

spring.datasource.hikari.poolName=Hikari
spring.datasource.hikari.auto-commit=false
spring.datasource.hikari.data-source-properties.cachePrepStmts=true
spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.data-source-properties.useServerPrepStmts=true