gpt4 book ai didi

hibernate - "org.hibernate.type.CollectionType : Created collection wrapper"是什么意思?

转载 作者:行者123 更新时间:2023-12-05 06:32:01 28 4
gpt4 key购买 nike

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 个表中加载所有数据。到底是怎么回事?正常吗?

感谢您的解释。

最佳答案

刚刚遇到这个问题并找到了解决方案。发生这种情况的原因是集合的类型为 Set。默认情况下,Java 会尝试检查对象是否已存在于集合中,方法是检查对象的所有属性是否都等于计数器对象,因此它将获取所有参与者集合。

我通过覆盖模型的 equalshash 方法修复了这个问题,并且只使用 ID 本身比较它们:

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RequestProduct requestProduct = (RequestProduct) o;
return Objects.equals(id, requestProduct.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

抱歉迟到了将近一年=/

关于hibernate - "org.hibernate.type.CollectionType : Created collection wrapper"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com