Im new to sql and im facing a problem with the below native query
public void saveOfflineBatchDetails(BigInteger user_id) {
em.createNativeQuery("INSERT INTO rst_offline_transaction_batch (created_date , user_id)" +
"VALUES('?1', ?2)")
.setParameter(1, new java.util.Date())
.setParameter(2, user_id)
.executeUpdate();
}
It doesn’t pass the values to the database. the created date should be the today’s date and time. Can anyone tell me whats wrong in this query.
Thanks a lot
You generally don’t include the single quotes for parameterised queries. Try:
(without the quotes around the
?1). Otherwise it’s hard (for both readers and parsers) to tell whether you wanted parameter 1 or the literal value?1to be inserted.You should also check the return value from
executeUpdate()to see if it thinks it affected any rows. This will probably give you zero but it’s worth checking anyway.And, finally, I think dates require special handling as per:
This is because the Java Date object is not a date at all but a timestamp – you need to ensure you select the correct temporal object type so that the right value is placed in the query.
So, in short, something like: