我有一個實體,如下所示;如何將此SQL語句正確轉換爲JPA標準查詢
@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...
}
我需要的JPA標準相當於下面的MySQL查詢
select * from cashhistory c
where (case when (c.funded = true) then 'SUCCESS' else 'FAILED' end) like '%SUCC%'
所以我做了這樣的JPA標準API表達;
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();
但它會在此行上引發以下異常TypedQuery<CashHistory> query = em.createQuery(cq);
。請參閱下面的StackTrace;
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]
有人可以請指我到哪裏我可能做錯了什麼?
這不回答我的問題,因爲你已經肯定提供哪些不是JPA2.0 CriteriaQuery中。也許你沒有正確理解我的問題? – CodeBurner