Home JPA equivalent for Hibernate NamedQuery's setCharacter(..), setDate(..) etc...?
Reply: 1

JPA equivalent for Hibernate NamedQuery's setCharacter(..), setDate(..) etc...?

useless Published in 2018-02-13 11:22:24Z

I want to set Date and Character in JPA. There are special methods in Hibernate as below:

Query q = session.getNamedQuery(namedQuery);
q.setDate(0, (Date) (params[i]));
q.setCharacter(1, params[i]);

Is there anything in JPA as setCharacter()?

I know there is setParameter() with Date but I do not know how to do it with Character. I want to convert it into JPA query parameter like:

Query q = getEntityManager().createNamedQuery(namedQuery);
// How to set Date and Character in JPA ?
pirho Reply to 2018-02-13 14:39:09Z

Your comment:

Ya but i have to differentiate it in to string,Date or Char using condition..

No, you do not have to differentiate. Assuming you are using jpa named query you just use setParam(..). Suppose you have named query like:

        query="SELECT dc FROM DateCharacter dc "
             +"WHERE dc.date<:date AND dc.character=:character")

Setting params goes then like:

Query query = em.createNamedQuery("DateCharacter.findOlderByChar");
query.setParameter("date", new Date());
query.setParameter("character", 'a');

If you then need casting, do it on setParam(..) like:

query.setParameter("character", (char)97); // ascii code for 'a'

And if you need any conditions to determine how to cast do it before setting the parameter.

But if you try to put a wrong type of param like string for character:

query.setParameter("character", "a string");

you will end up with an exception like:

Caused by: java.lang.ClassCastException: Cannot convert object "a string" of type "class java.lang.String" into an instance of "class java.lang.Character".

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324328 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO