I have searched through similar questions and found none helpful for this particularly weird problem of mine. The Exception message seems understandable, but a sensible reference to it's cause and where to make the appropriate corrections still beats me... I'm like WHY is the expected Type java.lang.Character???
I have a HashMap<String, Object>
called params
that holds all the search parameters that i need to use to filter results of a query to a MySQL Database, so here's how I build my query using JPA Criteria API.
The aim is to search all the specified dbColumns
for a String passed as the argument called search
List<String> dbColumns = Arrays.asList("firstname", "lastname", "username", "email", "mobilenumber", "dateJoined");
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Client> cq = cb.createQuery(Client.class);
Root<Client> entity = cq.from(Client.class);
cq.select(entity);
List<Predicate> predicates = new ArrayList<Predicate>();
if(params.containsKey("search") && StringUtils.isNotBlank(search)){
String search = String.valueOf(params.get("search"));
for(String column : dbColumns){
predicates.add(cb.like(cb.lower(entity.get(column).as(String.class)), "%"+search.toLowerCase()+"%"));
}
}
cq.where(predicates.toArray(new Predicate[]{}));
TypedQuery<Client> query = em.createQuery(cq); //<--- Error gets thrown here
return query.getResultList();
However I keep getting this error. Parameter value [%tunji%] did not match expected type [java.lang.Character] at this line TypedQuery<Client> query = em.createQuery(cq);
Please see StackTrace snippet below;
Caused by: java.lang.IllegalArgumentException: Parameter value [%tunji%] 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]
This actually confuses me because i'm NOT making any reference to java.lang.Character
at any point. Also all the MySQL columns except dateJoined
are of type VARCHAR which should map to java's String
Type.
The dbColumn dateJoined
is of MySQL type DATETIME hence why I used entity.get(column).as(String.class)
so that the String representation of the date will be compared with the search
String.
So i'm wondering what may be the cause of this error and how best to go about solving this problem. Any help would be highly appreciated.
EDIT:
In response to @Ish... Here's what the Client
entity looks like
@Entity
@Table(name="client")
@NamedQueries({
@NamedQuery(name="Client.findAll", query="SELECT c FROM Client c")
})
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;
@Column(length=255)
private String email;
@Column(length=50)
private String firstname;
@Column(length=50)
private String lastname;
@Column(length=20)
private String mobilenumber;
@Column(unique=true, length=255)
private String username;
@Column(nullable=false, name="datejoined")
private Date dateJoined;
//... getters ... and ... setters
}