HibernateException with setTimestamp, but works with setParameter

Here is the minimal version of the code that took me a long time to understand why it does not work:

Query q = session.createQuery(queryString); q.setTimestamp(0, new java.util.Date()); 

Error:

 Unset positional parameter at position: 0 

When I replaced setTimestamp() with setParameter() :

 Query q = session.createQuery(queryString); q.setParameter(0, new java.util.Date()); 

And it worked, but I can’t understand why. I am using hibernate 3.2.1.

EDIT:. Where did the publication with the other proposal come from? It was!

+6
source share
2 answers

Earlier there was an answer that suggested using java.sql.Timestamp instead of java.util.Date . With this small modification, the setTimestamp method works as expected.

+1
source

In your mapping, you may need to add the following annotation to your variable that holds the timestamp

@Temporal(TemporalType.TIMESTAMP)

This is why setParameter works (it just takes an object), but setTimestamp doesn't.

0
source

Source: https://habr.com/ru/post/901807/


All Articles