无语的hibernate tomcat spring hibernate 困扰几天的问题

做级联查询的时候,有时候启动tomcat,访问正常,有时候报java.lang.NullPointerException。
java.lang.NullPointerException
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.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3596)
at org.hibernate.type.EntityType.isEqual(EntityType.java:297)
at org.hibernate.type.ComponentType.isEqual(ComponentType.java:143)
at org.hibernate.engine.CollectionKey.equals(CollectionKey.java:50)
at java.util.HashMap.put(HashMap.java:376)
at org.hibernate.engine.StatefulPersistenceContext.addCollection(StatefulPersistenceContext.java:764)
at org.hibernate.engine.StatefulPersistenceContext.addUninitializedCollection(StatefulPersistenceContext.java:733)
at org.hibernate.type.CollectionType.getCollection(CollectionType.java:609)
at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:408)
at org.hibernate.type.CollectionType.resolve(CollectionType.java:402)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:120)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1649)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:120)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at com.v3.dao.EntityDAO.findBySqlAddClass(EntityDAO.java:159)
at com.v3.dao.EntityDAO$$FastClassByCGLIB$$3c0dbc4f.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617)
at com.v3.dao.EntityDAO$$EnhancerByCGLIB$$9881afb3.findBySqlAddClass()
at com.v3.service.EntityServiceImpl.findBySqlAddClass(EntityServiceImpl.java:194)
at com.v3.service.EntityServiceImpl$$FastClassByCGLIB$$ed745c95.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.v3.service.EntityServiceImpl$$EnhancerByCGLIB$$9be70e03.findBySqlAddClass()
at com.v3.service.AuthorService.getAuthorListPage(AuthorService.java:241)
at com.v3.action.AuthorController.index(AuthorController.java:83)
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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
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 com.v3.action.CharacterEncodingAdminFilter.doFilter(CharacterEncodingAdminFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
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 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(Thread.java:662)

代码:
    @OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_sec", columnDefinition = "id", insertable = false, updatable = false, nullable = true)
@NotFound(action=NotFoundAction.IGNORE)
public BookCategoryV3 getBookCategoryV3() {
    return bookCategoryV3;
}

public void setBookCategoryV3(BookCategoryV3 bookCategoryV3) {
    this.bookCategoryV3 = bookCategoryV3;
}

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "zc_id", columnDefinition = "bookId", insertable = false, updatable = false, nullable = true)
@OrderBy(clause = "dashang_all DESC")
@NotFound(action=NotFoundAction.IGNORE)
public BookDashangCount getBookDashangCount() {
    return bookDashangCount;
}

public void setBookDashangCount(BookDashangCount bookDashangCount) {
    this.bookDashangCount = bookDashangCount;
}

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "bookId", referencedColumnName = "zc_id", insertable = false,updatable = false, nullable = true)
@NotFound(action=NotFoundAction.IGNORE)
public List<ZwscBookFs> getZwscBookFs() {
    return zwscBookFs;
}

public void setZwscBookFs(List<ZwscBookFs> zwscBookFs) {
    this.zwscBookFs = zwscBookFs;
}
DAO代码:
public <T>List<T> findBySqlAddClass( String sql , Class<T> entityClass) {

    Session session = super.getSession();
    Query query = session.createSQLQuery(sql).addEntity(entityClass);
    List<Class<T>> list = query.list();
    releaseSession(session);
    return (List<T>) list;
}
执行query.list()的时候报错,我怀疑级联查询的时候有问题。但是不是每次都出错。
求大神帮忙。

2个回答

1、看看tomcat服务是否已经启动,如果启动,关掉后再运行
2、将eclipse里的tomcat服务器删掉,重新建立一个就可以了

xyzhanhh
xyzhanh 如果是带参数查询,就要先判断参数是否为空
接近 5 年之前 回复
dongguang111
dongguang111 关键我搞不懂,时好时坏。配置上都没什么问题。
接近 5 年之前 回复
dongguang111
dongguang111 不是tomcat的问题。应该是关联查询。
接近 5 年之前 回复

如果带参数查询,就要考虑参数是否为null,或者查询到的结果是否为null,这是我的一个项目中的daoImpl中一个带参查询,你参考一下
public List BrandList(BrandQueryBean brandQueryBean) throws BaseException {
List list = null;
Object[] args = null;
List params = new ArrayList();
StringBuffer hql = new StringBuffer("from BrandModelEntity b where (b.parent_brand_id is null or b.parent_brand_id='root')");
if (brandQueryBean != null) {
if (StringUtils.isNotBlank(brandQueryBean.getName())) {
hql.append(" and b.name like ?");
params.add("%" + brandQueryBean.getName() + "%");
}
}
if (params.size() > 0) {
args = params.toArray(new Object[params.size()]);
}
if (StringUtils.isNoneBlank(brandQueryBean.getSortField())) {
hql.append(" order by ").append(brandQueryBean.getSortField());
}
List<?> temp = super.queryForPage(hql.toString(), args, brandQueryBean);
if (null != temp) {
list = (List) temp;
}
return list;
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问