【发布时间】:2019-09-06 05:31:22
【问题描述】:
我用谷歌搜索了很多,Spring Boot(最新版本)可能没有延迟加载不起作用,这真的很奇怪。以下是我的代码片段:
我的资源:
public ResponseEntity<Page<AirWaybill>> searchAirWaybill(CriteraDto criteriaDto, @PageableDefault(size = 10) Pageable pageable{
airWaybillService.searchAirWaybill(criteriaDto, pageable);
return ResponseEntity.ok().body(result);
}
我的服务:
@Service
@Transactional
public class AirWaybillService {
//Methods
public Page<AirWaybill> searchAirWaybill(AirWaybillCriteriaDto searchCriteria, Pageable pageable){
//Construct the specification
return airWaybillRepository.findAll(spec, pageable);
}
}
我的实体:
@Entity
@Table(name = "TRACKING_AIR_WAYBILL")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@airWaybillId") //to fix Infinite recursion with LoadedAirWaybill class
public class AirWaybill{
//Some attributes
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_TRACKING_CORPORATE_BRANCH_ID")
private CorporateBranch corporateBranch;
}
在调试时,我仍然会加载所有延迟加载的属性。见下图。
我的一个问题是杰克逊会参与这种行为吗? 有没有我可能错过的激活延迟加载的方法?
编辑
另一个问题,调试器会不会参与破坏延迟加载?
编辑 2:
对于规范构建,我有:
public static Specification<AirWaybill> isBranchAirWayBill(long id){
return new Specification<AirWaybill>() {
@Override
public Predicate toPredicate(Root<AirWaybill> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.join("corporateBranch",JoinType.LEFT).get("id"),id);
}
};
}
【问题讨论】:
-
事务配置在哪里?在资源或服务上?
-
在服务上。我更新了问题
-
您的标准对corporateBranch有任何条件吗?如果是这样,您如何构建传递给存储库的规范?
-
是的,但有条件,甚至我删除了规范并只加载了一个页面,但我仍然看到问题
-
只是放弃调试器相关的问题,启用 Spring Boot SQL 查询日志记录:stackoverflow.com/questions/30118683/…
标签: java spring hibernate spring-boot spring-data-jpa