【发布时间】:2019-01-05 14:56:07
【问题描述】:
LazyLoading 似乎在我的应用程序中不起作用,我不知道为什么。
我有如下相关的实体:
public class Participant {
private Long id;
@OneToMany( mappedBy = "participant", fetch = FetchType.LAZY )
private Set<RequestProduct> requestProducts;
@OneToMany( mappedBy = "participant", fetch = FetchType.LAZY )
private Set<ParticipantRank> participantRanks;
@OneToMany( mappedBy = "participant", fetch = FetchType.LAZY )
private Set<RequestProductParticipant> requestProductParticipants;
}
还有
public class RequestProduct {
private Long id;
@Column
private String identifier;
@ManyToOne( fetch = FetchType.LAZY )
private Participant participant;
}
和存储库:
public interface RequestProductRepository extends JpaRepository<RequestProduct, Long> {
Optional<RequestProduct> findByIdentifier( String identifier );
}
以及服务方法:
@Transactional
@Service
public class ServiceImpl {
private RequestProductRepository repo;
public void modifyRequestProduct(String identifier){
//THE PROBLEM IS HERE
Optional<RequestProduct> product = repo.findByIdentifier( identifier );
}
}
当我调用findByIdentifier 方法时,似乎所有数据都已加载。我有这些堆栈跟踪:
[ taskExecutor-7] org.hibernate.type.CollectionType : Created collection wrapper: [org.module.module.models.Participant.requestProducts#1]
[ taskExecutor-7] org.hibernate.type.CollectionType : Created collection wrapper: [org.module.module.models.Participant.participantRanks#1]
[ taskExecutor-7] org.hibernate.type.CollectionType : Created collection wrapper: [org.module.module.models.Participant.requestProductParticipants#1]
然后调用 3 个大 select 查询,从 3 个表中的每一个加载所有数据。到底是怎么回事?
正常吗?
感谢您的解释。
【问题讨论】:
标签: hibernate spring-data-jpa lazy-loading one-to-many many-to-one