Java “gomobile.user u”不能是FROM子句的第一个声明

Java “gomobile.user u”不能是FROM子句的第一个声明,java,jpa,eclipselink,derby,Java,Jpa,Eclipselink,Derby,我试图在DerbyDB的模式gomobile中查询用户表的所有数据 我已经成功地建立了与数据库的连接,并创建了一个JPA实体,其所有列都对应于数据库表 @Entity @Table(name = "user", schema = "gomobile") public class User implements Serializable { private static final long serialVersionUID = 1L; // all columns p

我试图在DerbyDB的模式gomobile中查询用户表的所有数据

我已经成功地建立了与数据库的连接,并创建了一个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();
    }
}
我得到这个错误:

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

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


您应该使用从用户u中选择u,因为您要查询的实体默认为命名用户。

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


例如,关于如何在JPA中使用本机SQL的链接:

您是否尝试过在Derby数据库中以交互方式运行该1 SELECT语句?还有,什么是完整的Derby异常:@BryanPendleton我没有得到SQL异常。我有一个非法的辩论例外。如果我尝试通过JDBC运行上面的查询,我会得到一个错误。我必须使用gomobile.user中的SELECT*。我将更新我的线程。然后我得到异常描述:编译问题[从用户u中选择u]。[14,27]抽象架构类型“User”未知。这通常意味着查询中的名称与为实体定义的名称不匹配。如果您将logging设置为Finest,那么它应该显示在加载持久化单元时设置的内容。持久化单元是否适用于插入或其他任何操作?请小心,它区分大小写。
<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