开发项目的时候 在一个Job中执行了数据库操作, 用的是懒加载,但是如下错误

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ......., no session or session was closed

项目中使用 hibernate 注解的方式配置懒加载, 不是xml配置文件,

下面列出从网上搜集到的解决方案

1, 报如下错误

org.hibernate.LazyInitializationException: could not initialize proxy - no Session at

解决方法:

在web.xml中,配置springSevlet的位置,使用org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter,代码如下:

  <filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2, 一般是在定时任务quartzJob中,或者Junit测试中使用懒加载出现的错误, 报如下错误

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ......., no session or session was closed

解决方法:

添加事务配置注解。junit代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/demo-servlet.xml"})
@Transactional
public class Job {
...
}

3, 还有一种方法说是lazy=false, 我再 OneToMany | ManyToOne 注解中设置fetch=FetchType.EAGER 也不起作用, 不知道用配置文件的行不行

Hibernate + Spring (quartz) 整合懒(延迟)加载问题的更多相关文章

  1. 抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗?

    原文链接:抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗? 大家都知道,利用 Spring 整合 MyBatis,我们可以直接利用 @MapperScan 注解或者 @ ...

  2. AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖

    好了,现进入正题,在 AngularJs 实现动态(懒)加载主要是依赖于3个主JS文件和一段依赖的脚本. 实现的过程主要是引用3个主要的JS文件 <script src="angula ...

  3. AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转

    http://blog.csdn.net/zhangh8627/article/details/51752872 AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖 标签:  ...

  4. spring启动component-scan类扫描加载过程(转)

    文章转自 http://www.it165.net/pro/html/201406/15205.html 有朋友最近问到了 spring 加载类的过程,尤其是基于 annotation 注解的加载过程 ...

  5. Spring Boot自定义配置与加载

    Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...

  6. hibernate+spring的整合思路加实例(配图解)

    首先框架整合我感觉最难的是jar包的引入.因为不同框架的jar容易产生冲突.如果能排除这个因素我想说整合框架还是相对比较容易的. 我整合的框架的一个思想就是:各司其职.因为每个框架处理的事务或者是层次 ...

  7. Spring+Quartz 整合二:调度管理与定时任务分离

    新的应用场景:很多时候,我们常常会遇到需要动态的添加或修改任务,而spring中所提供的定时任务组件却只能够通过修改xml中trigger的配置才能控制定时任务的时间以及任务的启用或停止,这在带给我们 ...

  8. Spring学习笔记(1)——资源加载

    <!-- 占坑,迟点补充底层原理 --> Spring支持4种资源的地址前缀 (1)从类路径中加载资源——classpath: classpath:和classpath:/是等价的,都是相 ...

  9. Spring源码分析之-加载IOC容器

    本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spr ...

随机推荐

  1. [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  2. [LeetCode]题解(python):087-Scramble String

    题目来源: https://leetcode.com/problems/scramble-string/ 题意分析: 给定一个字符串,字符串展成一个二叉树,如果二叉树某个或多个左右子树颠倒得到的新字符 ...

  3. ORA-20000: ORU-10027: buffer overflow, limit of 10000 bytes

        要用dbms_output.put_line来输出语句,遇到以下错误: ERROR 位于第 1 行: ORA-20000: ORU-10027: buffer overflow, limit ...

  4. VC中判断指定窗口是否被其他窗口遮挡

    本来是想判断当前窗口是否在最前面,无奈办法用尽就是不行,于是想换个思路:判断指定窗口是否被其他窗口遮挡.然后掘网三尺,找到了这个: bool CTestTray2Dlg::IsCoveredByOth ...

  5. JAVA GUI学习 - JInternalFrame浮动窗口:可拖拽窗口(依赖于父窗口)

    public class JInternalFrameKnow extends JInternalFrame { public JInternalFrameKnow() { this.setBound ...

  6. Java开发岗位面试题

    看到一些java面试题,准备慢慢自己做出来试试. 一.Java基础 1. String类为什么是final的. 只有当字符串是不可变的,字符串池才有可能实现.字符串池的实现可以在运行时节约很多heap ...

  7. BZOJ 1062

    program candy bzoj1062; ; maxm=; maxn=; var n,len,m,i,p,t,l,r,c,d,q:longint; s:..,..maxn,..maxm] of ...

  8. swjtu 1962 A+B(模拟)

    题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1962 问题思路:考察编程基础的问题,涉及到字符串转为数字的问题. 代 ...

  9. Android 建造者(Builder)模式

    关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android  中 使用了 Bui ...

  10. Ext JS学习第五天 Ext_window组件(一)

    此文来记录学习笔记 •第一个组件:Ext.window.Window.对于组件,也就是Ext最吸引开发者的地方,那么我们要真正的使用Ext的组件,首先必须学会阅读API文档. –xtype:组件的别名 ...