您好我正在尝试做与文档中相同的事情
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch07.html#d5e5275
为什么会这样?
//In class Team
@ManyToOne
@JoinColumn(name = "Heat")
public Heat getHeat() {
return heat;
}
//In class Heat
@OneToMany(fetch = FetchType.LAZY, mappedBy = "heat")
@OrderColumn(name = "Heat_index")
public List<Team> getTeams() {
return teams;
}
创建了Heat_index列,但是我得到了这个异常。
org.hibernate.HibernateException: null index column for collection: no.domain.Heat.teams
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:766) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.collection.internal.PersistentList.readFrom(PersistentList.java:402) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1185) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:800) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:651) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:850) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2447) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2433) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.Loader.list(Loader.java:2258) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final]
at no..dao.HeatDaoImpl.getHeatsWithEventIdAndGrade(HeatDaoImpl.java:22) ~[HeatDaoImpl.class:na]
at no..service.HeatServiceImpl.getHeatsForEventWithGrade(HeatServiceImpl.java:84) ~[HeatServiceImpl.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_37]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_37]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_37]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_37]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) ~[org.springframework.aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[org.springframework.aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[org.springframework.aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) ~[org.springframework.transaction-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[org.springframework.aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[org.springframework.aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at $Proxy55.getHeatsForEventWithGrade(Unknown Source) ~[na:na]
at no..controller.EventController.getStartList(EventController.java:116) ~[EventController.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_37]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_37]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_37]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_37]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) ~[org.springframework.web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) ~[org.springframework.web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) ~[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) [servlet-api.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [org.springframework.web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [org.springframework.web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.30]
at no.stafetten.filter.RequestFilter.doFilter(RequestFilter.java:49) [RequestFilter.class:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.30]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) [catalina.jar:7.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) [tomcat-coyote.jar:7.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) [tomcat-coyote.jar:7.0.30]
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.30]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_37]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_37]
at java.lang.Thread.run(Thread.java:680) [na:1.6.0_37]
答案 0 :(得分:4)
从OneToMany批注中删除mappedBy属性并添加以下批注。
@JoinColumn(name="Heat")
使用OrderColumn时不支持mappedBy,除非您想自己设置属性。
如果您确实想自己设置该属性,则必须将以下内容添加到团队并自行增加索引。
@Column(name="Heat_index")
public int getHeatIndex() {
return heatIndex;
}