当我尝试使用hibernate 3.3.0进行简单的多对一连接时,我在createQuery
中获得了NPE。我已经消除了空表和不良实体映射,不存在的会话等可能性,但我是新手来进行休眠注释,所以我不能100%确定我的注释是正确的。我在想这里可能有一两个注释,或者我在这里缺少一个注释,或者是HQL本身的语法错误,但我不知道从这里可以去哪里。
我有两个表,EventsJobs
和EventsJobsTags
共享一个公共属性docId
。两者之间存在多对一关系,因此EventsJobs
中的一条记录可以在EventsJobsTags
中包含多条记录。
我的类(为简洁省略了import / getters / setters / constructors):
package com.x.y.model;
@Entity
@Table(name = "EventsJobs")
public class EventJob {
@JoinColumn(name="docId")
@Id
@Column(name = "docId")
private String docId;
@Column(name="title")
private String title;
@Column(name="url")
private String url;
@Column(name="docType")
private String docType;
@Column(name="fileName")
private String fileName;
}
和
package com.x.y.model;
@Entity
@Table(name = "EventsJobsTags")
public class EventJobTag {
@JoinColumn(name="docId")
@Id
@Column(name = "docId")
private String docId;
@Column(name = "taxonomyId")
private int taxonomyId;
@Column(name = "tag")
private String tag;
}
查询:
Query q = sessionFactory.getCurrentSession().createQuery("
from com.x.y.model.EventJobTag as et
left join com.x.y.model.EventJob as e
where e.docType = 'jobs' and et.taxonomyId in ('6079')")
.setString("docType", type)
.setString("search", search);
stacktrace:
SEVERE: Servlet.service() for servlet [xyz] in context with path [/abc] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:320)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.x.y.dao.SearchResultsDaoImpl.searchResults(SearchResultsDaoImpl.java:30)
at com.x.y.dao.SearchResultsDaoImpl.jobResults(SearchResultsDaoImpl.java:20)
at com.x.y.service.ResumeServiceImpl.listJobs(ResumeServiceImpl.java:45)
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:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy17.listJobs(Unknown Source)
at com.x.y.controller.ResumeController.taxonomySearch(ResumeController.java:163)
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:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
答案 0 :(得分:1)
您尚未在域实体上声明关联。您的EventJob应该具有对EventJobTag列表的OneToMany引用。如下:
@OneToMany(mappedBy="eventJob")
List<EventJobTag> tags;
如果希望Hibernate级联对映射集合中的实体所做的更改,则需要指定级联类型。
你的EventJobTag类应该有一个引用它有外键的EventJob的ManyToOne关联。
@ManyToOne
@JoinColumn(name="eventJobId")
EventJob job;
我不确定您的@JoinColumn(name="docId")
注释正在做什么,他们并没有附加任何内容。因此,除了我所描述的更改之外,您还需要删除它们。