org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话

时间:2014-04-29 05:03:49

标签: mysql database hibernate spring-mvc transactions

访问我声明的servlet时遇到此错误:

org.hibernate.LazyInitializationException: could not initialize proxy
- no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.confloorapp.domain.Floor_$$_javassist_0.setNumberOfEvents(Floor_$$_javassist_0.java)
at com.ifloor.services.endpoint.UpdateFloorEventServlet.doGet(UpdateFloorEventServlet.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
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.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.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
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.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at net.stax.appserver.webapp.RequestMonitorValve.invoke(RequestMonitorValve.java:35)
at net.stax.appserver.admin.StaxApplicationQueryValve.invoke(StaxApplicationQueryValve.java:49)
at net.stax.appserver.webapp.RequestSetupValve.invoke(RequestSetupValve.java:31)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:652)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

直接从Servlet获取服务时:

public class UpdateFloorEventServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    try {
        FloorService floorService = (FloorService)context.getBean("floorService");
        Floor floor = (Floor) floorService.findById (floorId);
        ...
}

@Transactional(propagation = Propagation.SUPPORTS)
@Service("floorService")
public class FloorServiceImpl implements FloorService {
  ...
}

由于

我发现问题与控制器无关,当我设置属性时它与之相关:

 floor.setNumberOfEvents    ((Long)numOfEvents);

@Entity
@Table(name="T_FLOOR")
public class Floor  {

@Id
@Column(name = "FLOOR_ID", unique = true, nullable = false)
private Integer id;
@Column(name = "FLOOR_KEY")
private String floorKey;
@Column(name = "NUMBER_OF_EVENTS")
private Long numberOfEvents;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public Long getNumberOfEvents() {
    return numberOfEvents;
}
public void setNumberOfEvents(Long numberOfEvents) {
    this.numberOfEvents = numberOfEvents;
}
public String getFloorKey() {
    return floorKey;
}
public void setFloorKey(String floorKey) {
    this.floorKey = floorKey;
}   
}

的MySQL

  CREATE TABLE `T_FLOOR` (
  `FLOOR_ID` int(11) NOT NULL AUTO_INCREMENT,
  `FLOOR_KEY` varchar(45) DEFAULT NULL,
  `FLOOR_DESC` varchar(45) DEFAULT NULL,
  `LOCATION_ID` int(11) DEFAULT NULL,
  `NUMBER_OF_EVENTS` bigint(20) DEFAULT NULL,
  `LAST_EVENT` timestamp NULL DEFAULT NULL,
  `LAST_UPDATE` timestamp NULL DEFAULT NULL,
  `LATITUDE` float DEFAULT NULL,
  `LONGITUDE` float DEFAULT NULL,
   PRIMARY KEY (`FLOOR_ID`),
   UNIQUE KEY `FLOOR_ID_UNIQUE` (`FLOOR_ID`),
   KEY `FK_floor_location` (`LOCATION_ID`),
    CONSTRAINT `FK_door_location` FOREIGN KEY (`LOCATION_ID`) REFERENCES `T_LOCATION` (`LOCATION_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

0 个答案:

没有答案