欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

异常的org.hibernate.LazyInitializationException: could not initialize proxy

程序员文章站 2022-03-02 14:37:55
...
昨天总是抛出这个异常,想想最简单的方法估计就是把fetch设置为FetchType.EAGER,可这未免也太影响了效率,在网上上尝试了几种方法人们都建议把fetch 设为 FetchType.EAGER,或者通过在web.xml中设置OpenSessionInViewFilter,结果都不凑效,在同事的指导下发现用fetch可以解决这一难题。这就是fetch的功能。我们可以把fetch与lazy="true"的关系类比为事务当中的编程式事务与声明式事务。
错误的hql语句

SELECT bioChemicalInfo FROM BioChemicalInfo AS bioChemicalInfo LEFT JOIN bioChemicalInfo.experiment AS experiment LEFT JOIN experiment.experimentProcesses experimentProcess LEFT JOIN experimentProcess.experimentProcessRecords

加入fetch以后

SELECT bioChemicalInfo FROM BioChemicalInfo AS bioChemicalInfo LEFT JOIN [color=blue]FETCH [/color]bioChemicalInfo.experiment AS experiment LEFT JOIN FETCH experiment.experimentProcesses experimentProcess LEFT JOIN FETCH experimentProcess.experimentProcessRecords