openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lu Jiang <jessye...@gmail.com>
Subject NullPointer Exception when using Case Expressions
Date Fri, 20 Aug 2010 08:12:41 GMT
Hi,all
I am experiencing the JPA 2.0 new features using openjpa 2.0,but
currently I was trapped.
I defined a entity class Student
@Entity
public class Student implements Serializable {
    @Id
    private Integer id;
...
    private double totalScore;
    private String rank;
    //getters and setters
}
When I tried to simply update the rank,it all works fine.
  public void updateRank(Integer id) {
       String jpql="UPDATE  Student  s "
                + "SET s.rank= 'F' "
                + " WHERE s.id = " + id;
        Query query = em.createQuery(jpql);
        query.executeUpdate();
    }

but when I tried to update the rank according to totalScore in the
following method.

  public void updateRank(Integer id) {
        String jpql = "UPDATE Student s "
                + "SET s.rank= "
                + " CASE WHEN s.totalScore<20 Then 'D' "
                + " WHEN s.totalScore>=20 AND s.totalScore<30 Then 'C' "
                + " WHEN s.totalScore>=30 AND s.totalScore<40 Then 'B' "
                + " WHEN s.totalScore>=40 AND s.totalScore<50 Then 'A' "
                + " Else 'N/A' "
                + " END "
                + " WHERE s.id = " + id;
        Query query = em.createQuery(jpql);
        query.executeUpdate();
    }

Error like below will occur.
Exception in thread "main" <openjpa-2.0.0-r422266:935683 nonfatal user
error> org.apache.openjpa.persistence.ArgumentException: null
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:827)
        at org.apache.openjpa.kernel.QueryImpl.updateAll(QueryImpl.java:898)
        at org.apache.openjpa.kernel.QueryImpl.updateAll(QueryImpl.java:894)
        at org.apache.openjpa.kernel.DelegatingQuery.updateAll(DelegatingQuery.java:582)
        at org.apache.openjpa.persistence.QueryImpl.executeUpdate(QueryImpl.java:348)
        at org.apache.geronimo.sample.jpa20.bean.Facade.updateRank(Facade.java:158)
        at org.apahce.geronimo.sample.jpa20.test.updateRank.main(updateRank.java:20)
Caused by: java.lang.NullPointerException
        at org.apache.openjpa.jdbc.kernel.exps.AndExpression.appendTo(AndExpression.java:68)
        at org.apache.openjpa.jdbc.kernel.exps.WhenCondition.appendTo(WhenCondition.java:70)
        at org.apache.openjpa.jdbc.kernel.exps.GeneralCaseExpression.appendTo(GeneralCaseExpression.java:114)
        at org.apache.openjpa.jdbc.sql.DBDictionary.appendUpdates(DBDictionary.java:2127)
        at org.apache.openjpa.jdbc.sql.DBDictionary.toBulkOperation(DBDictionary.java:2021)
        at org.apache.openjpa.jdbc.sql.DBDictionary.toUpdate(DBDictionary.java:1933)

Does anyone have idea of this kind of issue?Or I got something wrong
with the jpql sentence?Hope someone can give me some clue:)Thanks in
advance!

Mime
View raw message