Spring Data,JPA @OneToMany Lazy提取在Spring Boot中不起作用 [英] Spring Data, JPA @OneToMany Lazy fetch not working in Spring Boot

查看:465
本文介绍了Spring Data,JPA @OneToMany Lazy提取在Spring Boot中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FabricRollFabricDefect之间具有@OneToMany关系.

I have @OneToMany relationship between FabricRoll and 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

The problem is when I get FabricRoll by JpaRepository function

findAll()

findAll()

关联的FabricDefect也已加载.

我只想加载FabricRoll,并且在调用函数getFabricDefect()

I want to load only FabricRoll and FabricDefect should load when calling the function getFabricDefect()

FabricRollServiceImpl类

FabricRollServiceImpl class

@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);
}

}

断裂点:

控制台:

推荐答案

这似乎是调试工件.

在调试时,由于事务仍处于打开状态,因此监视的延迟加载的实体属性将在断点评估时加载.

At debugging time, because the transaction is still open, the watched lazy loaded entity properties will be loaded at the breakpoint evaluation time.

要检查生产"行为,应在断点之前插入em.detach语句,或使用日志记录(如Manza所建议),并检查em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(fabricRoll1.fabricDefects())在分离的实体上是否返回false.

To check the "production" behavior you should insert a em.detach statement just before the breakpoint or use logging (as suggested by Manza) and check em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(fabricRoll1.fabricDefects()) returns false on the detached entity.

(例如,通过声明@PersistenceContext private EntityManager em;记住要注入EntityManager)

(remember to inject EntityManager for example by declaring @PersistenceContext private EntityManager em;)

这篇关于Spring Data,JPA @OneToMany Lazy提取在Spring Boot中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆