Hibernate异常 - java.lang.NullPointerException

时间:2013-05-01 15:52:25

标签: hibernate

在一个自连接类中,我有两个父级(setter和checker),每个父级与模块类有一对多的关系。我想得到每种父类型的id,并且id也会获得与该id相关联的子(模块)。所以我一直在实现它:

员工

  public class Staff implements Serializable, 
  @Id
  @Column(name = "staff_id")
  private String staffId;

  @ManyToOne(cascade={CascadeType.ALL})
  @JoinColumn(name="checker_id")
  private Staff checker;

  @OneToMany(mappedBy="checker", orphanRemoval=true, cascade = CascadeType.ALL)
  private Set<Staff> setters = new HashSet<Staff>();

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="setter")
  private Set<Module> sModule = new HashSet<Module>();

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="checker")
  private Set<Module> cModule = new HashSet<Module>(); 

 //getters and setters}

模块

  public class Module implements Serializable{

 @ManyToOne(fetch=FetchType.LAZY) 
 @JoinColumn(name="staff_id", insertable=false, updatable=false)
 private Staff setter;

 @ManyToOne(fetch=FetchType.LAZY) 
 @JoinColumn(name="checker_id", insertable=false, updatable=false)
 private Staff checker; 

 //getters and setters }

DAO代码

    @Transactional
@SuppressWarnings("unchecked")
public Staff getWithModules(String staffId){

//Retrieve Staff
    Criteria crit = getCurrentSession().createCriteria(Staff.class);
    crit.add(Restrictions.eq("staffId", staffId));
    Staff staff = get(crit);

    //Retrieve the modules for the staff
    crit = getCurrentSession().createCriteria(Module.class);
    crit.add(Restrictions.eq("setter.staffId", staffId));
    crit.add(Restrictions.isNull("checkerId"));
    crit.addOrder(Order.asc("moduleId"));
    Set<Module> sModule = new LinkedHashSet<Module>(crit.list());
    staff.setsModule(sModule);  
    return staff;
}

所以在上面的代码中我试图获得与setter属性相关的模块,只是它出现了异常:

    java.lang.NullPointerException
com.project.professional.dao.StaffDAO.getWithModules(StaffDAO.java:62)
com.project.professional.dao.StaffDAO$$FastClassByCGLIB$$d033d033.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.project.professional.dao.StaffDAO$$EnhancerByCGLIB$$710c2495.getWithModules(<generated>)
com.project.professional.service.StaffServiceImpl.getWithModules(StaffServiceImpl.java:54)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy102.getWithModules(Unknown Source)
com.project.professional.controller.StaffController.showStaffModules(StaffController.java:83)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

显然,我现在这样做的方式并不完全正确,那么获得每位家长及其子女的最佳方式是什么?

编辑:使用错误突出显示第62行:

   staff.setsModule(sModule);  

0 个答案:

没有答案