Anadi B Pattanaik
Anadi B Pattanaik

Reputation: 1

JPA Select Query with composite primary key leads an exception

I have tried the below code for getting the single NotificationSubType Object. But finding exception like " org.apache.openjpa.persistence.ArgumentException: Encountered "from" at character 1, but expected: ["DELETE", "SELECT", "UPDATE"]."

Please help me on given problem. Code is given below : ... ...

try {

    Query query = this.em.createQuery("from NotificationSubType conc WHERE conc.id.alertTyC = ?1 and conc.id.alertSubTyC = ?2");

    query.setParameter(1, "MPPRINT");
    query.setParameter(2, "REIN");

    NotificationSubType trackIdResLst = (NotificationSubType)query.getSingleResult();

    if (null != trackIdResLst) {
        System.out.println("TRUE");
    } else{
        System.out.println("FALSE");
    }   
} catch (Exception e) {
    NotificationLogger.errorWithThrowable(e);
    throw new NotificationServiceException(e.getMessage());
}

...

Upvotes: 0

Views: 575

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

Why not read the exception? and read the JPA spec ?

JPQL statements have to start with SELECT, UPDATE or DELETE. Anything other than those is illegal (and yours starts with FROM for some reason). Put a SELECT and the candidate alias in front of it for more chance of success

Yes Hibernate bastardises the JPQL standard with HQL (which allows you to omit SELECT plus the alias) but you are using OpenJPA so you need to use JPA compliant queries, and besides it would be good practice even when using Hibernate to obey the standard!

Upvotes: 1

Related Questions