2014-03-06 2 views
0

user 테이블의 모든 데이터를 내 DerbyDB 스키마 gomobile에서 쿼리하려고합니다.'gomobile.user u'는 FROM 절의 첫 번째 선언이 될 수 없습니다.

데이터베이스에 대한 연결을 성공적으로 설정하고 데이터베이스 테이블에 해당하는 모든 열과 함께 JPA 엔터티를 만들었습니다. 내가 사용하는 경우

<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> 

편집

:

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에서 내 지속성 선언은 다음과 같습니다

@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입니다

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

당신이 당신의 더비 데이터베이스에 대해 대화 형으로 그냥 한 SELECT 문을 실행하려고 유무 : JPA에 네이티브 SQL을 사용하는 방법에 대한 예를 들어

링크? 또한 FULL Derby 예외는 무엇입니까? http://wiki.apache.org/db-derby/UnwindExceptionChain –

+0

@BryanPendleton 'SQL 예외'가 없습니다. 나는'IllegalArgumentException'을 얻는다. 위의 쿼리를 JDBC를 통해 실행하려고하면 오류가 발생합니다. 나는'SELECT * FROM gomobile.user'을 사용했습니다. 내 스레드를 업데이 트합니다. – Niklas

답변

3

gomobile.user라는 엔터티가 없습니다를, 그래서 당신은 당신의 JPQL 쿼리에서 사용할 수 없습니다. JPQL은 객체 기반이며 SQL에서와 같이 직접 테이블/스키마와 필드를 사용하지 않습니다.

quering하려는 엔티티의 이름이 기본적으로 "사용자"이므로 "SELECT u FROM User u"만 사용해야합니다.

+0

그러면 예외가 발생합니다 : [SELECT u FROM User u] 컴파일 문제. [14, 27] 추상 스키마 유형 'User'unknown '입니다.' – Niklas

+1

이것은 일반적으로 쿼리의 이름이 엔터티에 대해 정의 된 이름과 일치하지 않음을 의미합니다. 로깅을 Finest로 설정하면 지속성 단위를로드 할 때 설정되는 내용이 표시됩니다. 퍼시스턴스 유닛이 인서트 또는 다른 것을 위해 작동합니까? – Chris

+0

손톱 바로 위에! –

0

이 문제는 JPA에서 JPQL 구문을 사용하고 사용자 본인의 네이티브 SQL이 직접 허용되지 않기 때문에 발생합니다. 그러나 일반 SQL을 직접 사용할 수있는 쉬운 대안 인 엔티티 관리자가 지원하는 방법이 있습니다. http://www.thoughts-on-java.org/jpa-native-queries/

관련 문제