我尝试使用来自其他线程的建议解决此错误,例如使用@transactional注释函数,但这似乎没有任何效果。我也尝试过使用Hibernate.initialize(),但后来收到错误,“集合与任何会话无关”。
我不是最好的Java,所以我真的很感谢很容易理解的解释。
@Component
public class ItemEdmEntityConverter implements EdmEntityConverter<Item> {
private final ItemVersionDao itemVersionDao;
private final PolicyDao policyDao;
private final ClaimDao claimDao;
@Autowired
public ItemEdmEntityConverter(ItemVersionDao itemVersionDao, PolicyDao policyDao, ClaimDao claimDao) {
this.itemVersionDao = itemVersionDao;
this.policyDao = policyDao;
this.claimDao = claimDao;
}
@Override
public Entity toEdmEntity(Item domainEntity) {
Entity entity = new Entity();
addPrimitiveEdmProperty(entity, ID, domainEntity.getId());
addPrimitiveEdmProperty(entity, IS_PROOF_COMPLETED, getIsProofCompleted(domainEntity));
return entity;
}
@Transactional
private boolean getIsProofCompleted(Item domainEntity) {
Hibernate.initialize(domainEntity.getEvidence());
String[] requiredProof = new String[]{
Evidence.EvidenceType.PROOF_OF_DEVICE.toString(),
Evidence.EvidenceType.PROOF_OF_PURCHASE.toString()
};
Set<Evidence> allEvidence = domainEntity.getEvidence();
for (Evidence evidence : allEvidence) {
ArrayUtils.removeElement(requiredProof, evidence.getEvidenceType().toString());
}
return requiredProof.length == 0;
}
配置
@Configuration
@EnableTransactionManagement
public class AdaptersConfig {
@Autowired
private RestrictionsParser restrictionsParser;
@Autowired
private SortParser sortParser;
@Bean
public EdmEntityAdapterReader<Long>
evidenceEdmEntityAdapterReaderDelegatee(EvidenceDao evidenceDao, RestrictionsParser restrictionsParser, SortParser sortParser, ToEdmEntityConverter<Evidence> evidenceToEdmEntityConverter) {
return new EdmEntityAdapterReaderImpl<>(Evidence.class, evidenceDao, restrictionsParser, sortParser, evidenceToEdmEntityConverter);
}
}