Hi guys, I'm having problems with exact matches when doing NamedQuery.
I am currently using something like this:
@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from Entry e where e.name =:"+ Entry.NAME ) ... Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME); query.setParameter(Entry.NAME, myEntry.getName());
This works in most cases, however, I noticed that if the user passes a file name with a space at the end, namedQuery ignores this character. For example:
Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME); query.setParameter(Entry.NAME, myEntry.getName()+ " ");
Will return the same result as the request before. Bypassing my valid record validation. In other words, I would like the request to not return any record and handle the error later.
A workaround I could think of is single quotes associated with my parameter in namedQuery, for example:
@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from entry e where e.name =':"+ Entry.NAME "'")
However, it will be garbage of my code if String contains single quotes in it ...
Any ideas guys?
java jpa jpql named-query
flavio_yama
source share