当前位置: 首页 > 面试题库 >

使用Hibernate(JPA)查询Postgresql找不到表

汲昊空
2023-03-14
问题内容

我正在使用JPA和Hibernate和Postgresql开发应用程序。使用Netbeans向导,我从现有数据库中创建了实体类。这些类之一的摘录如下:

@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?这不是应该自动完成吗(毕竟我是从现有数据库中创建实体类的)?代码有什么问题?

谢谢!


问题答案:

Postgres(用于,不确定较新的版本)将表名转换为小写。这是首选的操作过程。如果您记录查询,您会看到hibernate名称可能是或可能不是您的表名(我想不是)。

老实说,如果您在Postgres上运行,则实际上应该正确配置hibernate模式,或者,正如我所看到的,应该将数据库规范化,因为表不应该发生名称空间冲突(从而消除了问题)。

//来自文章…

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

编辑的07/31/12:

必须通过以下方式对表的字段进行此更改:

对于@Column,请更改列的名称,然后添加转义的“”:

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

对于@JoinColumn,更改添加`的列的名称:

@JoinColumn(name = "`TP_MODEL`")

您将必须手动在出现错误的列上进行操作。



 类似资料:
  • 我使用Spring Boot 1.4.1和spring-boot-starter-data-jpa 当查询我的自定义方法时,比如'find byname(String name)',它不是缓存。

  • 问题内容: 我们对PostgreSQL中的继承以及在JPA中将其映射为实体存在疑问。我们的数据库和我们要映射的表是: 我们在Netbeans 7.1.2中使用自动工具将它们映射到实体。起初我认为仅添加就足够了 因此,它只是 扩大了 ,但无法正常工作。最好的方法是什么?提前致谢。 问题答案: JPA的继承概念基于普通表。它并没有真正“理解” PostgreSQL表继承的想法。这是使用旨在暴露功能的最

  • 我正在两个应用程序之间进行基准测试,一个是用构建的,另一个是用构建的。总体而言,差异不太大(小于一个数量级)。唯一的例外是当我通过主键获取记录时 使用JDBC的时间不超过10毫秒,使用Hibernate的时间从不低于60毫秒。我已经安装了ehcache,但是对于这个特定的查询,差异仍然是一样的<有没有什么我不知道的技巧? ---编辑 JPA映射和类是使用Hibernate工具构建的,它们没有什么特

  • 我创建了查询来根据日期间隔和房间类型搜索酒店的可用房间。当我运行此查询时,我得到以下错误: 组织。h2.jdbc。JdbcSQLException:未找到模式“R”;SQL语句: 有什么问题吗? 公共接口RoomRepository扩展JpaRepository{

  • 我正在使用hibernate envers来审核事件,当我调用repositoy.delete()或Repository.save()时,它可以很好地与Spring Data JPA存储库一起工作

  • 使用JPA2。0(通过Hibernate 4),我可以使用EntityManager在数据库中查询查询条件为主键的记录。查找(java.lang.Class entityClass,java.lang.Object primaryKey)。但是,如果查询条件不是主键(例如,选择city值为“london”的所有记录),我是否必须创建一个TypedQuery并显式提供SQL语句?我应该采取什么最佳做