Monday, March 28, 2011

Is there such thing CASE expression in JPQL?

Let say there is a table:

TableA:Field1, Field2, Field3

and associated JPA entity class

@Entity
@Table(name="TableA")
public class TableA{
  @Id
  @Column(name="Field1")
  private Long id;

  @Column(name="Field2")
  private Long field2;

  @Column(name="Field3")
  private Long field3;

  //... more associated getter and setter...
}

Is there any way to construct a JPQL statement that loosely translated to this SQL, ie how to translated the case expression to JPQL?

select field1,
case
  when field2 = 1 then 'One'
  when field2 = 2 then 'Two'
  else 'Other number'
end,
field3
from tableA;
From stackoverflow
  • Looking over the Java Persistence API it doesn't appear to have a construct for this. It looks like you're going to need a special getter function which will do the CASE statement functionality.

    Nordin : Thanks for the reply. I am not trying it yet, but, I wonder if the said getter can be used in a JPQL?
  • You can use the event listeners provided by Jpa to do something when you load one row of the db, ie:

    @Entity
    @Table(name = "TableA")
    public class TableA {
    
        @Id
        @Column(name = "Field1")
        private Long id;
    
        @Column(name = "Field2")
        private Long field2;
    
        @Column(name = "Field3")
        private Long field3;
    
        // ... more associated getter and setter...
    
        @Transient
        private String field4;
    
        @PostLoad
        private void onLoad() {
            if (field2 != null) {
                switch (field2.intValue()) {
                case 1:
                    field4 = "One";
                    break;
                case 2:
                    field4 = "Two";
                    break;
                default:
                    field4 = "Other Number";
                    break;
                }
            }
        }
    }
    

    (the field4 not persist in the db)

    (take this like an workaround to "non implemented feature in JPA" like case statements)

  • Did this solution work for anyone? It's not working for me. I'm using Eclipselink and I get the following error: unknown state or association field [simplifiedSideString] of class [com.entity.Trade]. Trade is the entity and simplifiedSideString is the @Transient field equivalent to field4 in the above example. To the extent of my knowledge, transient fields cannot be used in JPQL queries or at least I didn't succeed in using any yet.

  • There is certainly such thing in Hibernate so when you use Hibernate as your JPA provider then you can write your query as in this example:

        Query query = entityManager.createQuery("UPDATE MNPOperationPrintDocuments o SET o.fileDownloadCount = CASE WHEN o.fileDownloadCount IS NULL THEN 1 ELSE (o.fileDownloadCount + 1) END " +
                                                " WHERE o IN (:operations)");
        query.setParameter("operations", mnpOperationPrintDocumentsList);
    
        int result = query.executeUpdate();
    

0 comments:

Post a Comment