hibernate LazyInitializationException 这是非常诡计

a hibernate LazyInitializationException which is very trick

我有两段代码,几乎一模一样,只是位置不同,一段在RestLoginDLCtrl(控制器),一段在CouponManagerController(控制器),但有一段代码是对的,另一个抛出 org.hibernate.LazyInitializationException.

我知道如何解决我的问题。最让我困惑的是为什么一个可以,另一个是错误的,但它们几乎是相同的代码。为什么?任何帮助将不胜感激。

ConversionCode 是一个值对象,它有一个名为 codeRewards 的集合,而 managerService 是一个服务 class。

注:

终于,我发现了一些有趣的现象。当我使用ajax方式来posthttp请求时,两种方法都很好:

$.ajax({
    type: "POST",
    url: "/CouponInfo/useConversionCode.do",
    data: {
        code: key
    },
    dataType: "json",
    success: function (data) {

    }
});

但是当我使用form方式时,两种方法都没有work:Doeshibernate lazy初始化机制与http请求方式有关系吗?

<form action=""></form>

CouponManagerController控制器class:

    @Controller
    @RequestMapping(value = "${adminPath}/CouponInfo")
    public class CouponManagerController extends RestBaseCtrl {
        @Autowired
        CouponManagerService managerService;
        @ResponseBody
        public void useConversionCode(String code, HttpServletRequest request, HttpServletResponse response) {
            response.setContentType("text/json;charset=utf-8");
            try {
                LoginCustInfo info = (LoginCustInfo) request.getSession().getAttribute(SystemProperties.DUOLIJR_LOGIN_USER);
                ConversionCode conversionCode = managerService.queryConversionCodeByCode(code);
                if (conversionCode != null) {
                    managerService.useConversionCode(conversionCode, info.getCustId());
                    response.getWriter().print("[{\"success\":\"true\",\"message\":\"兑换成功\"}]");
                } else {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"无效兑换码\"}]");

                }
            } catch (ParseException e) {
                e.printStackTrace();
                try {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"兑换失败!请稍后重试!\"}]");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"兑换失败!请稍后重试!\"}]");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

RestLoginDLCtrl控制器class:

    @Controller
    @RequestMapping(value = "${adminPath}/yqxqrest/loginDLCtrl", method = RequestMethod.POST)
    public class RestLoginDLCtrl extends RestBaseCtrl {
        @Autowired
        CouponManagerService managerService;

        @RequestMapping(value = "useConversionCode")
        @ResponseBody
        public void useConversionCode(String code, HttpServletRequest request, HttpServletResponse response) {

            response.setContentType("text/json;charset=utf-8");
            try {
                LoginCustInfo info = (LoginCustInfo) request.getSession().getAttribute(SystemProperties.DUOLIJR_LOGIN_USER);
                ConversionCode conversionCode = managerService.queryConversionCodeByCode(code);
                if (conversionCode != null) {
                    managerService.useConversionCode(conversionCode, info.getCustId());
                    response.getWriter().print("[{\"success\":\"true\",\"message\":\"兑换成功\"}]");
                } else {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"无效兑换码\"}]");

                }
            } catch (ParseException e) {
                e.printStackTrace();
                try {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"兑换失败!请稍后重试!\"}]");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    response.getWriter().print("[{\"success\":\"false\",\"message\":\"兑换失败!请稍后重试!\"}]");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

CouponManagerServiceImpl 服务class:

    @Service("ActivitiesCouponsService")
    public class CouponManagerServiceImpl extends BaseServiceImpl implements CouponManagerService {

        @Autowired
        JdbcDao jdbcDao;
        @Autowired
        ActivitiesCouponsService couponsService;
        @Override
        public ConversionCode queryConversionCodeByCode(String code){
            ConversionCode conversionCode=null;
            String hql = " from ConversionCode where DELETE_FLAG=0 and state=0 and code='"+code+"'";
            List<ConversionCode> codes = jdbcDao.find(hql);
            if (codes.size()>0) {
                conversionCode=codes.get(0);
            }

            return conversionCode;
        }

        /**
         * 使用兑换码
         * @param conversionCode
         * @param custId
         * @throws ParseException
         */
        @Override
        public void useConversionCode(ConversionCode conversionCode,Integer custId) throws ParseException {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date now = new Date();
            Calendar endTime = Calendar.getInstance();
            endTime.setTime(sdf.parse(sdf.format(now)));

            Set<ConversionCodeReward> codeRewards=conversionCode.getCodeRewards();

            StringBuffer sb=new StringBuffer();
            String a="";
            for(ConversionCodeReward codeReward:codeRewards) {
                CustActivitiesInfo activitiesInfo = new CustActivitiesInfo();
                activitiesInfo.setCustId(custId);
                activitiesInfo.setCouponsType(codeReward.getCouponsType());
                String couponsName=couponsService.findUcTypeDictionaryBycode("COUPONS_TYPE", codeReward.getCouponsType() + "").getItemName();
                activitiesInfo.setCouponsName(couponsName);
                activitiesInfo.setActivitiesAmount(codeReward.getActivitiesAmount());

                endTime.add(Calendar.DAY_OF_YEAR, Integer.valueOf(codeReward.getValidPeriod()));
                endTime.add(Calendar.SECOND, -1);
                activitiesInfo.setStartDate(sdf.parse(sdf.format(now)));
                activitiesInfo.setEndDate(endTime.getTime());
                activitiesInfo.setTaskAction("兑换码");
                activitiesInfo.setUseMeetAmount(codeReward.getUseMeetAmount());
                activitiesInfo.setUsePlanType(codeReward.getUsePlanType());
                activitiesInfo.setUsePlanPeriod(codeReward.getUsePlanPeriod());
                activitiesInfo.setState(1);
                jdbcDao.saveObject(activitiesInfo);

                sb.append("1张"+codeReward.getCouponsName()+"、");

            }
            conversionCode.setState(1);
            jdbcDao.updateObject(conversionCode);


        }

    }

ConversionCode 值对象class:

    @Entity
    @Table(name = "conversion_code")
    public class ConversionCode extends BaseEntity implements java.io.Serializable{

        private int id;
        private String code;
        private Date endDate;
        private int state;
        private String remark;
        private Set<ConversionCodeReward> codeRewards;

        public ConversionCode(int id,String code,Date endDate,int state,String remark){
            super();
            this.id=id;
            this.code=code;
            this.endDate=endDate;
            this.state=state;
            this.remark=remark;
        }
        public  ConversionCode(){

        }

        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "id", unique = true, nullable = false)
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Column(name = "code")
        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        @Column(name = "end_date")
        public Date getEndDate() {
            return endDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        @Column(name = "state")
        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }


        @ManyToMany(fetch=FetchType.LAZY,mappedBy="codes")
        public Set<ConversionCodeReward> getCodeRewards() {
            return codeRewards;
        }

        public void setCodeRewards(Set<ConversionCodeReward> codeRewards) {
            this.codeRewards = codeRewards;
        }

        @Column(name = "remark")
        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }

ConversionCodeReward 值对象class:

    @Entity
    @Table(name = "conversion_code_reward")
    public class ConversionCodeReward extends BaseEntity implements java.io.Serializable{

        private Integer id;
        private String couponsName;
        private int couponsType;
        private double activitiesAmount;
        private int validPeriod;
        private double useMeetAmount;
        private String usePlanType;
        private int usePlanPeriod;
        private Set<ConversionCode> codes;

        public ConversionCodeReward(Integer id,String couponsName,int couponsType,double activitiesAmount,
                                    int validPeriod,double useMeetAmount,String usePlanType,int usePlanPeriod){
            super();
            this.id=id;
            this.couponsName=couponsName;
            this.couponsType=couponsType;
            this.activitiesAmount=activitiesAmount;
            this.validPeriod=validPeriod;
            this.useMeetAmount=useMeetAmount;
            this.usePlanType=usePlanType;
            this.usePlanPeriod=usePlanPeriod;
        }

        public ConversionCodeReward(){

        }

        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "id", unique = true, nullable = false)
        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Column(name = "coupons_name")
        public String getCouponsName() {
            return couponsName;
        }

        public void setCouponsName(String couponsName) {
            this.couponsName = couponsName;
        }

        @Column(name = "coupons_type")
        public int getCouponsType() {
            return couponsType;
        }

        public void setCouponsType(int couponsType) {
            this.couponsType = couponsType;
        }

        @Column(name = "activities_amount")
        public double getActivitiesAmount() {
            return activitiesAmount;
        }

        public void setActivitiesAmount(double activitiesAmount) {
            this.activitiesAmount = activitiesAmount;
        }

        @Column(name = "valid_period")
        public int getValidPeriod() {
            return validPeriod;
        }

        public void setValidPeriod(int validPeriod) {
            this.validPeriod = validPeriod;
        }

        @Column(name = "use_meet_amount")
        public double getUseMeetAmount() {
            return useMeetAmount;
        }

        public void setUseMeetAmount(double useMeetAmount) {
            this.useMeetAmount = useMeetAmount;
        }

        @Column(name = "use_planType")
        public String getUsePlanType() {
            return usePlanType;
        }

        public void setUsePlanType(String usePlanType) {
            this.usePlanType = usePlanType;
        }

        @Column(name = "use_planPeriod")
        public int getUsePlanPeriod() {
            return usePlanPeriod;
        }

        public void setUsePlanPeriod(int usePlanPeriod) {
            this.usePlanPeriod = usePlanPeriod;
        }

        @ManyToMany(cascade={CascadeType.MERGE,CascadeType.REFRESH},fetch=FetchType.LAZY)
        @JoinTable(name = "code_and_reward",
                joinColumns = { @JoinColumn(name = "reward_id") },
                inverseJoinColumns = { @JoinColumn(name = "code_id") })
        @Where(clause="DELETE_FLAG=0")
        public Set<ConversionCode> getCodes() {
            return codes;
        }

        public void setCodes(Set<ConversionCode> codes) {
            this.codes = codes;
        }
    }

异常堆栈跟踪:

    2015-12-16 15:38:34,059 ERROR (org.hibernate.LazyInitializationException:19) - failed to lazily initialize a collection of role: com.yiqixiangqian.entity.ConversionCode.codeRewards, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.yiqixiangqian.entity.ConversionCode.codeRewards, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
        at com.yqxqfront.coupon.service.impl.CouponManagerServiceImpl.useConversionCode(CouponManagerServiceImpl.java:152)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
        at com.yiqixiangqian.common.log.LogAspect.around(LogAspect.java:31)
        at sun.reflect.GeneratedMethodAccessor255.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:96)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at com.sun.proxy.$Proxy119.useConversionCode(Unknown Source)
        at com.yqxqfront.rest.controller.RestLoginDLCtrl.useConversionCode(RestLoginDLCtrl.java:665)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:155)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.access.channel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:144)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.yqxqfront.rest.filter.SessionFilter.doFilter(SessionFilter.java:186)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.yqxqfront.filter.ImageVerifyCodeFilter.doFilter(ImageVerifyCodeFilter.java:66)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:615)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1774)
        at java.lang.Thread.run(Thread.java:662)
    Hibernate: select custmessag0_.UID as UID245_, custmessag0_.CREATE_DATE as CREATE2_245_, custmessag0_.CREATED_BY as CREATED3_245_, custmessag0_.DELETE_FLAG as DELETE4_245_, custmessag0_.LAST_UPDATE_DATE as LAST5_245_, custmessag0_.UPDATE_BY as UPDATE6_245_, custmessag0_.VERSION as VERSION245_, custmessag0_.CONTENT as CONTENT245_, custmessag0_.CUST_ID as CUST9_245_, custmessag0_.ISCHECK as ISCHECK245_, custmessag0_.PUB_DATE as PUB11_245_, custmessag0_.TITLE as TITLE245_, custmessag0_.TYPE as TYPE245_ from cust_message custmessag0_ where custmessag0_.CUST_ID=? and custmessag0_.ISCHECK=? and custmessag0_.DELETE_FLAG=0
    failed to lazily initialize a collection of role: com.yiqixiangqian.entity.ConversionCode.codeRewards, no session or session was closed

位于CouponManagerController控制器中的useConversionCode()方法可以:

位于RestLoginDLCtrl控制器中的useConversionCode()方法错误并抛出LazyInitializationException异常:

终于,我发现了一些有趣的现象。当我使用 ajax 方式来 post HTTP 请求时,两种方法都很好:

        $.ajax({
        type: "POST",
        url: "/CouponInfo/useConversionCode.do",
        data: {
            code: key
        },
        dataType: "json",
        success: function (data) {

        }
    });

但是当我使用表单方式时,两种方法都不起作用:

<form action=""></form>

hibernate lazy初始化机制和http请求方式有关系吗?

我知道这个问题 occured.Suddenly,我发现 web.xml 文件中有一个 OpenSessionInViewFilter。

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>   
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
    </filter-class>
<init-param>
  <param-name>singleSession</param-name>
  <param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping> 

在我的表单post请求方式中,我没有添加.do请求后缀,所以OpenSessionInViewFilter没有起作用,所以出现了LazyInitializationException。

但是ajaxpost请求方式添加了.do请求后缀,所以OpenSessionInViewFilter起作用了,没有出现LazyInitializationException