`
jerryli_vip
  • 浏览: 838 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

LazyInitializationException: could not initialize proxy - no Session

阅读更多
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
分享到:
评论

相关推荐

    基于SSH框架的BBS论坛JavaEE项目源码

    7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 ...

    集成spring的hibernate懒加载

    当你尝试在Controller层或者视图层访问懒加载的属性时,如果Session已经关闭("no Session..."错误),就会抛出`org.hibernate.LazyInitializationException`。这是因为懒加载的代理对象需要Session来执行数据库查询...

    Hibernate配置常见错误

    错误表现:在Session关闭后尝试访问懒加载属性,抛出“org.hibernate.LazyInitializationException: could not initialize proxy - no Session”异常。 解决方案:理解并合理使用Open Session in View(OSIV)模式...

    java开源论坛jeebbs系统源码包

    7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 ...

    derby.jar 。。

    java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.AutoloadedDriver40 导致的原因: 在azkaban的server和executor中缺少一个叫derby.jar的包

    Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser

    标题 "Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser" 提示了一个Java运行时错误,这通常意味着在尝试加载或初始化`JRStyledTextParser`类时遇到了问题。`JRStyledTextParser`是...

    S2S3H3整合以及泛型Dao与Service封装

    2.2.3.1+spring-framework-3.1.0+hibernate-distribution-3.6.8+JSON+MySQL+Annotation,并且对Dao和Service进行了封装,内含.jar包,并且解决了一对多双向关联的could not initialize proxy - no Session错误,同时...

    Plsql 12连接Oracle时出现Could not initialize oci.dll解决方案 oracle 客户端

    在使用PL/SQL Developer 12连接Oracle数据库时,可能会遇到“Could not initialize oci.dll”这样的错误提示。这个错误通常表明系统无法找到或正确加载Oracle客户端的oci.dll库文件,这是Oracle Instant Client的一...

    jasperreport maven打包后找不到字体解决方案

    jasperreport 用maven打包后找不到字体解决方案 net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font

    derby_ui_plugin_1.1.1

    Derby UI Plugin 1.1.1 是一个专门为Java开发者设计的用户界面插件,它在Java学习过程中能提供极大的便利。这个插件版本号为1.1.1,暗示了它可能包含了一些修复和改进,以提升用户体验和兼容性。...

    jacob_1.14.3.rar

    内涵jacob_1.14.3-x64.dll;jacob_1.14.3-64.jdk(maven地址);解决方法:Could not initialize class com.jacob.com.ComThread

    Android代码-RetrofitUrlManager

    // When building OkHttpClient, the OkHttpClient.Builder() is passed to the with() method to initialize the configuration OkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder...

    linux上实现视频截图

    在Linux操作系统上实现视频截图是一项常见的任务,尤其对于开发者来说,可能需要在处理多媒体内容时进行这样的操作。这里我们将深入探讨如何使用JavaCPP库在32位Linux环境下完成这一目标。 JavaCPP是一个强大的Java...

    mysql-5.7.17-winx64的安装、配置和初始化

    注意:如果仅使用`mysqld --initialize-insecure`可能会导致错误。 #### 六、注册MySQL为Windows服务 1. **安装服务**:同样在命令提示符中执行命令: ```cmd mysqld install MySQL --defaults-file="%MYSQL_HOME...

    Plsql Developer连接Oracle时出现Could not initialize oci.dll解决方案

    在使用PL/SQL Developer这款强大的Oracle数据库管理工具时,有时可能会遇到“Could not initialize oci.dll”的错误提示,这通常是由于应用程序与Oracle客户端组件之间的兼容性问题导致的。本文将详细介绍如何解决这...

    mysql8.0 X64 安装文件和手动安装脚本

    D:\mysql-8.0.11-winx64\bin&gt;mysqld --initialize-insecure --console D:\mysql-8.0.11-winx64\bin&gt;mysqld --install mysql811 D:\mysql-8.0.11-winx64\bin&gt;net start mysql811 D:\mysql-8.0.11-winx64\bin&gt;mysql -u...

    oracle_plsql连服务端时 Initialization error could initialize错误处理方法

    解决:oracle_plsql连服务端时 Initialization error could initialize错误处理方法

    msp430对刚接触的人来说很好资料

    - 如果遇到类似“Fatal error: Failed to re-initialize, Session aborted!”的错误提示,请检查项目配置是否正确。 - 重启开发板或重新插拔USB线,有时也能解决问题。 #### 六、总结 通过上述步骤,新手可以快速...

    安装mysql-8.0.19-winx64遇到的问题:Can’t create directory ‘xxxx\Database\’

    如图,用管理员运行,在命令行窗口输入mysqld --initialize --console,执行初始化命令,出现了mysqld: Can’t create directory ‘D: ystem tool\mysql-8.0.19-winx64\Database’ (OS errno 2 – No such

Global site tag (gtag.js) - Google Analytics