JPQL:使用TABLE_PER_CLASS继承无法识别Postgres实体

时间:2015-09-03 20:18:03

标签: java postgresql jpa spring-data-jpa jpql

我有一个看起来像这样的课程:

@Entity(name="DashboardWidget")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class DashboardWidget 
{
    public enum Type {USER_COUNT, ...};

    @Column(name="COMPANY_ID")
    protected Long companyId;

    @Enumerated(EnumType.STRING)
    protected Type type;

    ...

此实体背后没有表格。这些表与每个子类实体一起存在。这些类看起来像这样:

@Entity
@Table(name = "WIDGET_USER_COUNT")
public class UserCountWidget extends DashboardWidget 
{
    ...

使用Spring Data JPA,我在我的存储库中有以下内容:

List<DashboardWidget> findByCompanyId(Long companyId);

这很好用,List包含不同子conlete类的实例。然后,我有一个用例,我只想Set枚举Type,这样我就不必加载完整的实体。所以,我把它添加到我的Spring Data存储库中:

@Query("SELECT w.type FROM DashboardWidget w WHERE w.companyId = :companyId")
Set<DashboardWidget.Type> getWidgetTypes(@Param("companyId") Long companyId);

然而,当调用它时,我收到以下错误:

Internal Exception: org.postgresql.util.PSQLException: ERROR: relation "dashboardwidget" does not exist
  Position: 18
Error Code: 0
Call: SELECT TYPE FROM DASHBOARDWIDGET WHERE (COMPANY_ID = ?)
    bind => [8888810010000000000]
Query: ReportQuery(referenceClass=DashboardWidget sql="SELECT TYPE FROM DASHBOARDWIDGET WHERE (COMPANY_ID = ?)")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:682)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:250)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2738)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllReportQueryRows(ExpressionQueryMechanism.java:2675)
    at org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:848)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1751)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
    ... 99 more
Caused by: org.postgresql.util.PSQLException: ERROR: relation "dashboardwidget" does not exist
  Position: 18
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:1007)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:642)
    ... 119 more

我不太了解JPQL,知道它是否与我的查询有关,或者是否有某种神奇的Spring Data正在做findByCompanyId方法工作。我可以做些什么来使getWidgetTypes方法有效吗?

1 个答案:

答案 0 :(得分:0)

好像你用引号创建了表,但试图在没有引号的情况下查询它。 Postgres是case-insensitive for unquoted identifiers。如果是这种情况,您需要考虑引号并使用像@Entity(name="\"DashboardWidget\"")@NamedQueries这样的smth,以免混淆区分大小写。

一般的好习惯是不要在psql中使用表的引号,除非你有合理的理由。