2014-03-06 19 views
0

我試圖在我的DerbyDB的模式gomobile中查詢我的user表的所有數據。'gomobile.user u'不能成爲FROM子句的第一個聲明

我已成功建立與我的數據庫的連接並創建了一個JPA實體,其所有列都對應於數據庫表。

@Entity 
@Table(name = "user", schema = "gomobile") 
public class User implements Serializable { 
    private static final long serialVersionUID = 1L; 

    // all columns 

    public static List<User> getAll() { 
     String queryString = "SELECT u FROM gomobile.user u"; 
     EntityManager em = Persistence.createEntityManagerFactory("Eclipselink").createEntityManager(); 
     return em.createQuery(queryString, User.class).getResultList(); 
    } 
} 

這是stracktrace:

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT u FROM gomobile.user as u]. 
[14, 41] 'gomobile.user as u' cannot be the first declaration of the FROM clause. 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1625) 
    at com.sap.sapchat.jpa.entities.User.getAll(User.java:45) 
    at com.sap.sapchat.jpa.entities.InitDatabase.main(InitDatabase.java:50) 
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException 
Exception Description: Problem compiling [SELECT u FROM gomobile.user as u]. 
[14, 41] 'gomobile.user as u' cannot be the first declaration of the FROM clause. 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603) 
    ... 3 more 

在persistence.xml我的持久性聲明如下所示:

<persistence-unit name="Eclipselink" transaction-type="RESOURCE_LOCAL"> 
    <class>jpa.entities.User</class> 
    <properties> 
     <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/gomobile;create=true" /> 
     <property name="javax.persistence.jdbc.user" value="gomobile" /> 
     <property name="javax.persistence.jdbc.password" value="mypassword" /> 
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" /> 
    </properties> 
</persistence-unit> 

編輯

如果我使用:

String queryString = "SELECT * FROM gomobile.user u"; 

我得到這個錯誤:

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT * FROM gomobile.user u]. 
[38, 38] A select statement must have a FROM clause. 
[7, 7] The left expression is missing from the arithmetic expression. 
[9, 38] The right expression is not an arithmetic expression. 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1625) 
    at com.sap.sapchat.jpa.entities.User.getAll(User.java:75) 
    at com.sap.sapchat.jpa.entities.InitDatabase.main(InitDatabase.java:64) 
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException 
Exception Description: Syntax error parsing [SELECT * FROM gomobile.user u]. 
[38, 38] A select statement must have a FROM clause. 
[7, 7] The left expression is missing from the arithmetic expression. 
[9, 38] The right expression is not an arithmetic expression. 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278) 
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102) 
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603) 
    ... 3 more 
+0

您是否嘗試過對您的Derby數據庫交互的運行只是1條SELECT語句?另外,什麼是完整的Derby異常:http://wiki.apache.org/db-derby/UnwindExceptionChain –

+0

@BryanPendleton我沒有得到'SQL異常'。我得到一個'IllegalArgumentException'。如果我嘗試通過JDBC運行上面的查詢,我得到一個錯誤。我必須使用'SELECT * FROM gomobile.user'。我會更新我的線程。 – Niklas

回答

3

沒有名爲gomobile.user實體,所以你不能在你的JPQL查詢中使用它。 JPQL是基於對象的,並不像使用SQL那樣直接使用表/模式和字段。

您應該只使用「SELECT u FROM User u」,因爲您正在查詢的實體默認命名爲「User」。

+0

那麼我得到'異常說明:編譯問題[選擇你從用戶u]。 [14,27]抽象模式類型'User'是未知的。 – Niklas

+1

這通常意味着查詢中的名稱與實體定義的名稱不匹配。如果將日誌記錄設置爲最佳,則應顯示加載持久單元時設置的內容。持久性單元是否適用於插入或其他任何東西? – Chris

+0

就在指甲上! –

0

此問題是因爲JPA預計JPQL語法和像您的原生SQL不允許直接。但是有一種實體管理器支持的方法,在普通SQL可以直接使用的情況下,這是一種簡單的選擇。

例如鏈接瞭解如何使用本地SQL在JPA:http://www.thoughts-on-java.org/jpa-native-queries/