【问题标题】:'gomobile.user u' cannot be the first declaration of the FROM clause'gomobile.user u' 不能是 FROM 子句的第一个声明
【发布时间】:2014-03-06 07:43:11
【问题描述】:

我正在尝试在我的 DerbyDB 的架构 gomobile 中查询我的 user 表的所有数据。

我已成功建立到我的数据库的连接并创建了一个 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

【问题讨论】:

  • 您是否尝试过仅针对您的 Derby 数据库交互式地运行 1 条 SELECT 语句?另外,什么是 FULL Derby 异常:wiki.apache.org/db-derby/UnwindExceptionChain
  • @BryanPendleton 我没有收到SQL exception。我得到一个IllegalArgumentException。如果我尝试通过 JDBC 运行上述查询,则会收到错误消息。我必须使用SELECT * FROM gomobile.user。我会更新我的帖子。

标签: java jpa eclipselink derby


【解决方案1】:

没有名为 gomobile.user 的实体,因此您不能在 JPQL 查询中使用它。 JPQL 是基于对象的,并且不像在 SQL 中那样直接使用表/模式和字段。

您应该只使用“SELECT u FROM User u”,因为您查询的实体默认命名为“User”。

【讨论】:

  • 那么我得到Exception Description: Problem compiling [SELECT u FROM User u]. [14, 27] The abstract schema type 'User' is unknown.
  • 这通常意味着查询中的名称与为实体定义的名称不匹配。如果您将日志记录设置为最好,它应该显示加载持久性单元时设置的内容。持久性单元是否适用于插入或其他任何东西?
  • 小心,区分大小写。
【解决方案2】:

这个问题是因为 JPA 需要 JPQL 语法,而像你这样的原生 SQL 是不允许直接使用的。但是,实体管理器支持一种方法,这是一种可以直接使用普通 SQL 的简单替代方法。

例如关于如何在 JPA 中使用原生 SQL 的链接:http://www.thoughts-on-java.org/jpa-native-queries/

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2014-05-26
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2015-09-10
    • 2016-11-17
    相关资源
    最近更新 更多