2012-12-19 80 views
5

嗨,我試圖做同樣的事情文檔Proplem與@OrderColumn的Hibernate 4.1

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] 

回答

4

從OneToMany註釋中移除mappedBy屬性並添加以下注釋。

@JoinColumn(name="Heat") 

使用OrderColumn時,mappedBy不受支持,除非您想自己設置屬性。

如果你想自己設置的屬性,你就必須添加以下內容團隊和增量索引自己。

@Column(name="Heat_index") 
public int getHeatIndex() { 
    return heatIndex; 
}