浏览 1833 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2014-05-08
报错: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 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |