我有 @OneToMany
的关系连接了 FabricRoll
和 FabricDefect
。
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "fabric_roll_id", referencedColumnName = "fabric_roll_id")
private Set<FabricDefect> fabricDefects = new HashSet<>();
问题在于我通过JpaRepository
函数获取FabricRoll
时,关联的FabricDefect
也会被加载。
我希望仅加载FabricRoll
,并且只有当调用getFabricDefect()
函数时才加载FabricDefect
。
FabricRollServiceImpl
类
@Component
public class FabricRollServiceImpl implements IFabricRollService{
@Autowired
FabricRollRepository fabricRollRepository;
@Transactional(propagation = Propagation.REQUIRED)
@Override
public List<FabricRoll> getAllFabricRoll() {
FabricRoll fabricRoll1 = new FabricRoll();
fabricRoll1.setBatchNo("34344");
fabricRoll1.setLotNo("425");
fabricRoll1.setPoNo("42");
fabricRoll1.setRollLength(2343);
fabricRoll1.setRollNo("356");
fabricRoll1.setRollWidth(60);
fabricRoll1.setStyleNo("354");
FabricDefect fabricDefect = new FabricDefect();
fabricDefect.setDefectNote("note");
fabricDefect.setDefectPoint(3);
fabricDefect.setSegment(3);
fabricDefect.setYard(42);
Set<FabricDefect> fabricDefects = new HashSet<>();
fabricDefects.add(fabricDefect);
fabricRoll1.setFabricDefects(fabricDefects);
addFabricRoll(fabricRoll1);
FabricRoll fabricRoll = null;
return fabricRollRepository.findAll();
}
@Override
public void addFabricRoll(FabricRoll fabricRoll) {
fabricRollRepository.save(fabricRoll);
}
}
@Transactional
注解,您会发现会出现LazyInitializationException
,这意味着集合没有被加载。 - Manza