2012-07-21 74 views
5

我正在開發一個使用JPA與Hibernate和Postgresql的應用程序。使用Netbeans嚮導,我從現有數據庫創建實體類。從這個類的一個提取物如下:使用休眠(JPA)查詢Postgresql沒有找到表

@Entity 
@Table(name = "ADM_TYPES") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "AdmTypes.findAll", query = "SELECT a FROM AdmTypes a"), 
    @NamedQuery(name = "AdmTypes.findByCType", query = "SELECT a FROM AdmTypes a WHERE a.cType = :cType"), 
    @NamedQuery(name = "AdmTypes.findByVlType", query = "SELECT a FROM AdmTypes a WHERE a.vlType = :vlType"), 
    @NamedQuery(name = "AdmTypes.findByDsType", query = "SELECT a FROM AdmTypes a WHERE a.dsType = :dsType"), 
    @NamedQuery(name = "AdmTypes.findByVlStatus", query = "SELECT a FROM AdmTypes a WHERE a.vlStatus = :vlStatus"), 
    @NamedQuery(name = "AdmTypes.findByCTypePrefix", query = "SELECT a FROM AdmTypes a WHERE a.cType like :cTypePrefix")}) 
public class AdmTypes implements Serializable { 

在EJB我有:

@Stateless 
@LocalBean 
public class ModelManagement { 
    @PersistenceContext 
    private EntityManager em; 

    public List<AdmTypes> listAdmTypesPrefix(String prefix){ 
     TypedQuery<AdmTypes> query = em.createNamedQuery("AdmTypes.findByCTypePrefix", AdmTypes.class); 
     query.setParameter("cTypePrefix", "'%"+prefix+"%'"); 
     return query.getResultList(); 
    } 

當我嘗試執行ModelManagement.listAdmTypesPrefix("APREFIX")它生成以下錯誤:

javax.ejb.EJBException 
    at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193) 
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091) 
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879) 
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039) 
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
    at $Proxy192.listAdmTypesPrefix(Unknown Source) 
    at com.librethinking.simmodsys.ejb.__EJB31_Generated__ModelManagement__Intf____Bean__.listAdmTypesPrefix(Unknown Source) 
    at com.librethinking.MockServlet.processRequest(MockServlet.java:64) 
    at com.librethinking.MockServlet.doGet(MockServlet.java:94) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ERROR: relation "adm_types" does not exist 
    Position: 136 
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1367) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1295) 
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:265) 
    at com.librethinking.simmodsys.ejb.ModelManagement.listAdmTypesPrefix(ModelManagement.java:39) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) 
    ... 32 more 
Caused by: org.hibernate.exception.SQLGrammarException: ERROR: relation "adm_types" does not exist 
    Position: 136 
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
    at $Proxy194.executeQuery(Unknown Source) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1953) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:829) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) 
    at org.hibernate.loader.Loader.doList(Loader.java:2438) 
    at org.hibernate.loader.Loader.doList(Loader.java:2424) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2254) 
    at org.hibernate.loader.Loader.list(Loader.java:2249) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1248) 
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) 
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:256) 
    ... 55 more 
Caused by: org.postgresql.util.PSQLException: ERROR: relation "adm_types" does not exist 
    Position: 136 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122) 
    ... 70 more 

我知道Postgres對於大寫的表名很討厭,但是如何將查詢更改爲指向「ADM_TYPES」而不是AdmTypes?不應該自動完成(畢竟我從現有數據庫創建實體類)?代碼有什麼問題?

謝謝!

+0

如果您刪除'from'「'%」+前綴+「%'」'? (它看起來不正確)。 – esej 2012-07-21 21:07:38

+0

不,這就是它應該完成「類似」比較的方式。 – 2012-07-21 21:52:54

回答

10

Postgres(用於,不確定更新)將表名轉換爲小寫。這是首選的操作程序。如果你記錄你的查詢,你會看到hibernate可能會或可能不會引用你的表名(我猜它不是)。

Hibernate saving User model to Postgres

老實說,如果你在運行的Postgres你真的應該要麼配置Hibernate正確,或者像我想看看它,你的正常化數據庫表不應該有一個命名空間衝突(從而消除問題)。

//從文章...

@Entity 
@Table(name="\"User\"") 
public class User { 
    ... 
} 

EDITED 12年7月31日:

這種變化必須採取措施以下列方式將表的字段:

對於@Column,更改列的名稱加入逃逸「:

@Column(name = "\"C_MODEL\"") 

對於@JoinColumn,更改列添加'的名字:

@JoinColumn(name = "`TP_MODEL`") 

你將不得不做手工上會發生錯誤的列。

+0

我會試試這個,讓你知道! – 2012-07-21 21:52:27

+2

Urk,那太可怕了。 Hibernate應該默認引用表名。我個人認爲Hibernate缺陷並沒有這麼做 - 如果它本應以這種方式工作,那肯定是一個NetBeans模型生成器錯誤。 – 2012-07-22 02:59:38

+0

你是對的@CraigRinger,這聽起來像是一個錯誤,似乎已經被報告 – 2012-07-23 21:31:47