2015-09-03 76 views
0

承認我有一個類,看起來像這樣:JPQL:Postgres的實體不與TABLE_PER_CLASS繼承

@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數據JPA,我有我的倉庫以下:

List<DashboardWidget> findByCompanyId(Long companyId); 

這個偉大的工程,並在列表中包含了不同的子conrete類的實例。然後我有一個用例,我只想要Type枚舉中的Set,這樣我就不必加載完整的實體。所以,我說這對我的春節,數據倉庫:

@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方法的工作?

+0

由於Eclipselink正在查詢DashboardWidget描述符,所以看起來像一個錯誤。您使用的是哪個EclipseLink版本? – Chris

+0

@Chris Eclipselink版本2.5.2 – dnc253

回答

0

看起來你用引號創建了表,但試圖在沒有它們的情況下查詢它。 Postgres是case-insensitive for unquoted identifiers。如果是這種情況,您需要考慮報價並使用像@Entity(name="\"DashboardWidget\"")@NamedQueries這樣的不合格,以免混淆區分大小寫。

好的做法一般不是在psql中使用引號,除非你有足夠的理由。

+0

我沒有帶引號的表格。沒有DashboardWidget表。這就是它炸燬的原因。它試圖查詢一個不存在的表。我的問題是如何編寫JPQL,以便知道如何查詢子類/實體使用的表。 Spring Data正在以某種方式工作。 – dnc253