SourceVisor
SourceVisor

Reputation: 1996

How to properly convert this SQL case statement to JPA Criteria Query

I have an entity as shown below;

@Entity
@Table(name="cashhistory")  
@NamedQueries({
    @NamedQuery(name="CashHistory.findAll", query="SELECT c FROM CashHistory c")
})

public class CashHistory implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false)
    private boolean funded;

    //... other fields
    //... getters & setters...
}

I need the JPA Criteria equivalent of the following MySQL query

select * from cashhistory c 
where (case when (c.funded = true) then 'SUCCESS' else 'FAILED' end) like '%SUCC%'

So I made a JPA Criteria API expression like this;

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<CashHistory> cq = cb.createQuery(CashHistory.class);
Root<CashHistory> entity = cq.from(CashHistory.class);
cq.select(entity);

Expression<String> expr = cb.selectCase()
    .when(cb.equal(entity.<Boolean>get("funded"), cb.literal(true)), cb.literal("SUCCESS"))
    .otherwise(cb.literal("FAILED")).as(String.class);

cq.where(cb.like(expr, "%SUCC%"));
TypedQuery<CashHistory> query = em.createQuery(cq);
return query.getResultList();

But it throws the following exception at this line TypedQuery<CashHistory> query = em.createQuery(cq);. See the StackTrace below;

Caused by: java.lang.IllegalArgumentException: Parameter value [%SUCC%] did not match expected type [java.lang.Character]
    at org.hibernate.ejb.AbstractQueryImpl.validateParameterBinding(AbstractQueryImpl.java:370) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:343) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:370) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.criteria.CriteriaQueryCompiler$1$1.bind(CriteriaQueryCompiler.java:194) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:247) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:622) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:96) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

Can someone please point me to where I'm probably doing something wrong?

Upvotes: 0

Views: 3408

Answers (2)

Ish
Ish

Reputation: 4154

Have you seen this issue from Hibernate?

I also tried your code and it looks like it won't work with String typed selectCase, and instead use Integer as workaround.

Something like this code should work:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CashHistory> cq = cb.createQuery(CashHistory.class);
    Root<CashHistory> entity = cq.from(CashHistory.class);
    cq.select(entity);

    Expression<Integer> caseExpr = cb.selectCase()
            .when(cb.equal(entity.get("funded"), true), 1)
            .otherwise(2).as(Integer.class);


    cq.where(cb.equal(caseExpr, 1));
    TypedQuery<CashHistory> query = em.createQuery(cq);
    query.getResultList();

Upvotes: 1

Kumaresan Perumal
Kumaresan Perumal

Reputation: 1956

you have to do like this and concentrate on the following link for named and native query http://www.tutorialspoint.com/jpa/jpa_jpql.htm

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(  "Eclipselink_JPA" );
          EntityManager entitymanager = emfactory.createEntityManager();
          Query query = entitymanager.
          createQuery("select c from cashhistory c where (case when (c.funded = true) then 'SUCCESS' else 'FAILED' end) like '%SUCC%'");
          List<String> list = query.getResultList();

          for(String e:list) {
             System.out.println("Object :"+e);
          }

Upvotes: 0

Related Questions