检索具有多对多关系的实体子集

时间:2014-02-20 00:55:13

标签: java mysql spring hibernate jpa

在使用hibernate,jpa和MySQL的spring mvc应用程序中,我在两个实体之间有一个ManyToMany关系:ProviderFacilityAddress。我正在尝试填充位于特定Providers的所有FacilityAddress的列表,但是,当调用此代码时,我当前收到以下错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:  
Unknown column 'providerti0_.provider_id' in 'field list'

我不知道这个错误是否是在实体级别jpa级别引起的。有人可以告诉我如何修复我的代码,这样错误就消失了吗?

请注意,所有必需的代码都在此帖子中,但其中一些代码位于我提供链接的文件共享站点。例如:
Provider entity code is at this link FacilityAddress code is at this link Person entity code is at this link BaseEntity code is at this link ProviderTitle code is at this link NamedEntity code is at this link

以下是控制器代码的相关部分,它启动事件序列,最终在调用时触发错误:

@RequestMapping("/calendar")
public String showCalendar(@RequestParam("day") String day, org.springframework.web.context.request.WebRequest webRequest, Model model) {
    //omitting some irrelevant code
    //get list of providers
    List<Provider> ps = (List<Provider>) clinicService.findProvidersByFacilityAddressId(7);
    for(int g=0;g<ps.size();g++){
        System.out.println(" ----------- provider name is: "+ps.get(g).getFirstName());
    }

    return "appointments/calendar";
}

这是clinService中的方法,由上面的代码调用:

@Override
@Transactional
public Collection<Provider> findProvidersByFacilityAddressId(int id) throws DataAccessException {
    return providerRepository.findProvidersByFacilityAddressId(id);
}

以下是从上一步调用的providerRepository(JpaProviderRepository.java)中的方法:

@Override
public Collection<Provider> findProvidersByFacilityAddressId(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    System.out.println("<<<<<<<<<<<<<<<<<<<<<<<< inside Jpa Repository findByFacilityId() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    Query query = this.em.createQuery("SELECT DISTINCT provider FROM Provider provider left join fetch provider.facilities pf WHERE pf.id =:id");
    System.out.println("========================= Just set query. ");
    query.setParameter("id", id);
    System.out.println("------------------------- Just added parameter to query. ");
    List<Provider> p = (List<Provider>) query.getResultList();
    System.out.println("------------------------- Just created ProviderCompany object from query result. ");
    return p;
}

这是在MySQL数据库中创建相关对象的sql:

CREATE TABLE IF NOT EXISTS provider_titles(
  id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(80),
  INDEX(name)
);

CREATE TABLE IF NOT EXISTS providers(
  id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  first_name VARCHAR(80),
  last_name VARCHAR(80),
  title_id INT(4) UNSIGNED,
  comments VARCHAR(300),
  FOREIGN KEY (title_id) REFERENCES provider_titles(id),
  INDEX(last_name)
);

CREATE TABLE IF NOT EXISTS facilityAddresses(
  id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  company_id int(11) UNSIGNED NOT NULL,
  type_id int(11) UNSIGNED NOT NULL,
  facilityname varchar(50),
  address varchar(200),
  city varchar(200),
  state_id int(11) UNSIGNED NOT NULL,
  zip varchar(50),
  isHQ bool,
  FOREIGN KEY (company_id) REFERENCES providerCompanies(id), 
  FOREIGN KEY (type_id) REFERENCES locationTypes(id),
  FOREIGN KEY (state_id) REFERENCES states(id)
);

CREATE TABLE IF NOT EXISTS providerOfficeJunction(
  id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  provider_id int(11) UNSIGNED NOT NULL,
  providerOffice_id int(11) UNSIGNED NOT NULL,
  FOREIGN KEY (provider_id) REFERENCES providers(id),
  FOREIGN KEY (providerOffice_id) REFERENCES facilityAddresses(id)
);

这是完整的堆栈跟踪:

<<<<<<<<<<<<<<<<<<<<<<<< inside Jpa Repository findByFacilityId() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
========================= Just set query. 
------------------------- Just added parameter to query. 
Hibernate: select distinct provider0_.id as id1_29_0_, facilityad2_.id as id1_12_1_, provider0_.first_name as first2_29_0_, provider0_.last_name as last3_29_0_, provider0_.title_id as title4_29_0_, facilityad2_.address as address2_12_1_, facilityad2_.city as city3_12_1_, facilityad2_.company_id as company7_12_1_, facilityad2_.facilityname as facility4_12_1_, facilityad2_.isHQ as isHQ5_12_1_, facilityad2_.type_id as type8_12_1_, facilityad2_.state_id as state9_12_1_, facilityad2_.zip as zip6_12_1_, facilities1_.provider_id as provider1_29_0__, facilities1_.providerOffice_id as provider2_24_0__ from providers provider0_ left outer join providerOfficeJunction facilities1_ on provider0_.id=facilities1_.provider_id left outer join facilityAddresses facilityad2_ on facilities1_.providerOffice_id=facilityad2_.id where facilityad2_.id=?
Hibernate: select providerti0_.id as id1_28_3_, providerti0_.name as name2_28_3_, providerti0_.provider_id as provider3_28_3_, provider1_.id as id1_29_0_, provider1_.first_name as first2_29_0_, provider1_.last_name as last3_29_0_, provider1_.title_id as title4_29_0_, emails2_.provider_id as provider3_29_5_, emails2_.id as id1_27_5_, emails2_.id as id1_27_1_, emails2_.name as name2_27_1_, emails2_.provider_id as provider3_27_1_, providerti3_.id as id1_28_2_, providerti3_.name as name2_28_2_, providerti3_.provider_id as provider3_28_2_ from provider_titles providerti0_ left outer join providers provider1_ on providerti0_.provider_id=provider1_.id left outer join provider_email emails2_ on provider1_.id=emails2_.provider_id left outer join provider_titles providerti3_ on provider1_.title_id=providerti3_.id where providerti0_.id=?
WARN  SqlExceptionHelper - SQL Error: 1054, SQLState: 42S22
ERROR SqlExceptionHelper - Unknown column 'providerti0_.provider_id' in 'field list'
INFO  DefaultLoadEventListener - HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
WARN  LoadContexts - HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@76f10c77<rs=com.mysql.jdbc.JDBC4ResultSet@59441dc0>
WARN  CollectionLoadContext - HHH000160: On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [5] entries
WARN  warn - Handler execution resulted in exception
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:635) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.samples.knowledgemanager.util.CallMonitoringAspect.invoke(CallMonitoringAspect.java:80) ~[CallMonitoringAspect.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at $Proxy43.findProvidersByFacilityAddressId(Unknown Source) ~[na:na]
    at org.springframework.samples.knowledgemanager.service.ClinicServiceImpl.findProvidersByFacilityAddressId(ClinicServiceImpl.java:185) ~[ClinicServiceImpl.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at $Proxy46.findProvidersByFacilityAddressId(Unknown Source) ~[na:na]
    at org.springframework.samples.knowledgemanager.web.EncounterController.showCalendar(EncounterController.java:173) ~[EncounterController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) [servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73) [datatables-core-0.9.2.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at com.github.dandelion.datatables.extras.servlet2.filter.DatatablesFilter.doFilter(DatatablesFilter.java:71) [datatables-servlet2-0.9.2.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.42]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.42]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.42]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) [tomcat-coyote.jar:7.0.42]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.42]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) [tomcat-coyote.jar:7.0.42]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_29]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_29]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_29]
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2036) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:899) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2117) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3927) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:460) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:429) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:206) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:262) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1092) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1019) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:648) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:468) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1107) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:963) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:910) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2522) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2508) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2333) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:264) ~[hibernate-entitymanager-4.2.1.Final.jar:4.2.1.Final]
    at org.springframework.samples.knowledgemanager.repository.jpa.JpaProviderRepositoryImpl.findProvidersByFacilityAddressId(JpaProviderRepositoryImpl.java:103) ~[JpaProviderRepositoryImpl.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 77 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'providerti0_.provider_id' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_29]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_29]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156) ~[mysql-connector-java-5.1.27.jar:na]
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2323) ~[mysql-connector-java-5.1.27.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    ... 121 common frames omitted

1 个答案:

答案 0 :(得分:1)

以下是我的想法。

当我看到

CREATE TABLE IF NOT EXISTS provider_titles(
  id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(80),
  INDEX(name)
);

基本上表id和name中有两个字段。 当我查看映射该类找到的实体

public class ProviderTitle extends NamedEntity {

    @ManyToOne
    @JoinColumn(name = "provider_id")
    private Provider provider;

@MappedSuperclass

public class NamedEntity extends BaseEntity {

    @Column(name = "name")
    private String name;

@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

使用那个层次

实体ProviderTitle有3个属性[id,name,provider],列名为[id,name, provider_id ],表只定义了2个属性,因此查询将失败

查看失败查询的代码似乎就是这样。

Hibernate:选择providerti0_.id为id1_28_3_,providerti0_.name为name2_28_3_, providerti0_.provider_id 为provider3_28_3_,provider1_.id为id1_29_0_,provider1_.first_name为first2_29_0_,provider1_.last_name为last3_29_0_,provider1_ .title_id为title4_29_0_,emails2_.provider_id为provider3_29_5_,emails2_.id为id1_27_5_,emails2_.id为id1_27_1_,emails2_.name为name2_27_1_,emails2_.provider_id为provider3_27_1_,providerti3_.id为id1_28_2_,providerti3_.name为name2_28_2_,providerti3_.provider_id as provider3_28_2_ from provider_titles providerti0_ left outer join providers1 _ on providerti0_.provider_id = provider1_.id left external join provider_email emails2_ on provider1_.id = emails2_.provider_id left outer join provider_titles providerti3_ on provider1_.title_id = providerti3_。 id where providerti0_.id =?

正如您所看到的,表没有列名provider_id,这就是查询无法检索结果的原因,请考虑检查ProviderTitle以删除提供程序属性或重新创建架构。这是根本原因。

<强>更新

提供商有ManyToOne到ProviderTitle

public class Provider extends Person {

    @ManyToOne
    @JoinColumn(name="title_id")
    protected ProviderTitle title;

public class ProviderTitle extends NamedEntity {

    @ManyToOne
    @JoinColumn(name = "provider_id")
    private Provider provider;

ProviderTitle将ManyToOne提供给提供者。

所以有两个单一的关系,每个都需要自己的列,考虑使用mappedBy,因为provider_id不是表provider_titles中的一列,如下所示:

   public class ProviderTitle extends NamedEntity {

        @OneToMany(mappedBy="title")
        private Collection<Provider> provider;