LazyInitializationException: could not initialize proxy - no Session

sanc51 2014-05-07 08:33:09
SpringMVC4+JPA+Hibernate4,设置fetch = FetchType.LAZY后,
报错:org.hibernate.LazyInitializationException: could not initialize proxy - no Session

我不想设置为 fetch = FetchType.EAGER,如何设置fetch = FetchType.LAZY,成功加载所需要的数据?
敬请高人指点。

配置文件如下:

web.xml
-----------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<display-name>Web Application</display-name>

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>zanwo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>zanwo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------------------------------------------------------------------------------

spring-servlet.xml
-----------------------------------------------------------------------------------------------------------------------------------------------------

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- active @Controller mode -->
<mvc:annotation-driven />

<mvc:default-servlet-handler />

<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/"/>

<context:annotation-config />

<context:component-scan base-package="com.zanwo" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPU" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="800000"/>
</bean>

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>
</props>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------------------------------------------------

persistence.xml
-----------------------------------------------------------------------------------------------------------------------------------------------------
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mysql" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
-----------------------------------------------------------------------------------------------------------------------------------------------------

其中两个类,shop(店铺)和商品(product)是一对多关系,我是这样定义:
Product.java
@Entity
@Table(name = "product")
public class Product extends BaseEntity implements Serializable {

private static final long serialVersionUID = 4386742079736072275L;

@Id
@GeneratedValue
@Column(name = "prod_id")
private Long prodId;

@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "shop_id")
private Shop shop;

@Column(name = "prod_name")
private String prodName;

@Column(name = "prod_desc")
private String prodDesc;

getter() and setter().....
}


Shop.java
@Entity
@Table(name = "shop")
public class Shop extends BaseEntity implements Serializable {
private static final long serialVersionUID = 2949762130923624926L;
@Id
@GeneratedValue
@Column(name = "shop_id")
private Long shopId;

@Column(name = "shop_name")
private String shopName;

@Column(name = "shop_desc")
private String shopDesc;

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "shop")
private Set<Product> products = new HashSet<Product>(0);

getter() and setter().....

}

当页面访问${product.shop.shopName}就会报错:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
...全文
243 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
之奇一昂 2014-05-09
  • 打赏
  • 举报
回复
OpenSessionInViewFilter一般都采用6楼这样吧
nicewonders 2014-05-09
  • 打赏
  • 举报
回复
配置一个OpenSessionInViewFilter,延长session生命周期。
sanc51 2014-05-09
  • 打赏
  • 举报
回复
快来帮忙啊,我的分还没送出去呢! 我在service里,这样写就可以: Product product = productDAO.findById(100); product.getShop(); logger.debug("product : " + product); 但我把打log那一行(上面三行代码的最后一行)去掉就不行,同样的 no session Exception,这该怎么办?log打出来很多, 而且log level 也会改的呀
sanc51 2014-05-08
  • 打赏
  • 举报
回复
先顶起来 ......
sanc51 2014-05-08
  • 打赏
  • 举报
回复
引用 2 楼 nicewonders 的回复:
缺少transactionManager配置,一般配置在数据源附近,需要spring的事务管理去开启session或者自己用hibernate管理session.
有配置的 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>
nicewonders 2014-05-08
  • 打赏
  • 举报
回复
缺少transactionManager配置,一般配置在数据源附近,需要spring的事务管理去开启session或者自己用hibernate管理session.
本项目是作者当时花费了3个多月的时间完成的.在此拿出来给大家分享,请珍惜作者的劳动成果,用心研读! JEEBBS最新版本采用hibernate3+spring mvc+spring3+freemarker技术架构,重新设计了jeebbs,重新架构后的JEEBBS性能得到了很大的提升,功能相比jeebbs v1.0也丰富了许多。 jeebbsV4.0功能列表 1、论坛APP 2、登录更改shiro登录认证以及记住我 3、在线人数、时长统计 4、提供用户接口、其他系统用户接口调用设置以及接口管理(可与jeecms系列软件无缝对接实现单点登录) 5、用户自定义字段 6、禁用ip、id发帖、回帖 7、注册成功自动登录 8、设置在线活跃度等级 9、手机模板方案设置 10、最近登录过(三天,一周、一个月、三个月、半年)查询 11、类似微信团队号(与用户沟通账户以及推送系统消息) 12、QQ登录 jeebbsV4.0修复以及完善部分 1.权限的访问的地址链接 2.图片太大显示不全问题 3.会员组设置附件上线没有用以及其他相关设置无效 4.附件上传经常上传不了 5.发帖文字内容不能居中、居左、居右 编辑器字体、大小、插入图片、排序列表无效、左浮动、右浮动 6.用户自定义头像错误 7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 10.注册会员的时候提示邮箱的格式不对 11.登录设置邮箱密码文本框改成密码框
JEEBBS最新版本采用hibernate3+spring mvc+spring3+freemarker技术架构,重新设计了jeebbs,重新架构后的JEEBBS性能得到了很大的提升,功能相比jeebbs v1.0也丰富了许多。 jeebbsV4.0功能列表 1、论坛APP 2、登录更改shiro登录认证以及记住我 3、在线人数、时长统计 4、提供用户接口、其他系统用户接口调用设置以及接口管理(可与jeecms系列软件无缝对接实现单点登录) 5、用户自定义字段 6、禁用ip、id发帖、回帖 7、注册成功自动登录 8、设置在线活跃度等级 9、手机模板方案设置 10、最近登录过(三天,一周、一个月、三个月、半年)查询 11、类似微信团队号(与用户沟通账户以及推送系统消息) 12、QQ登录 jeebbsV4.0修复以及完善部分 1.权限的访问的地址链接 2.图片太大显示不全问题 3.会员组设置附件上线没有用以及其他相关设置无效 4.附件上传经常上传不了 5.发帖文字内容不能居中、居左、居右 编辑器字体、大小、插入图片、排序列表无效、左浮动、右浮动 6.用户自定义头像错误 7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 10.注册会员的时候提示邮箱的格式不对 11.登录设置邮箱密码文本框改成密码框

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧