JPA Hibernate PostgreSQL current_date doesn't work -
i have postgresql 9.3.6 database following query defined:
create or replace view calls_today select cc.* call_config cc cc.created_at >= current_date; the view used on web portal developed play framework + hibernate via jpa 2. seems work fine except date related queries.
if server restarted today, view looks working today. tomorrow, see on web page calls last 2 days. next day, 1 more, 3 days , on. if issue query on psql client, results fine, current day.
i must missing something, doesn't query results cached (as each day result grows) rather current_date fixed @ day of server restart. kind of prepared statement, don't know.
the data pulled database following jpa 2 api:
private static<t extends ioutgoingcallconfig> result getcalls(class<t> entityclass) { check.argument.isnotnull(entityclass, "entityclass"); list<callitem> calls = new arraylist<>(); entitymanager em = jpa.em(); criteriabuilder cb = em.getcriteriabuilder(); criteriaquery<t> cq = cb.createquery(entityclass); root<t> rootentry = cq.from(entityclass); criteriaquery<t> = cq.select(rootentry); typedquery<t> allquery = em.createquery(all); for(t entity: allquery.getresultlist()) { calls.add(new callitem(entity)); } return jsonsuccess(calls); } i have tested via psql following prepared query:
prepare mystmt select current_time; doesn't suffer problem. each execution reveals updated current server time:
execute mystmt; having simple view below:
create or replace view my_current_time select current_time; causes same problems. 1 query has been run, return value same :(
it might related definition of current_time , current_date. postgresql 9.4 documentation:
since these functions return start time of current transaction, values not change during transaction. considered feature: intent allow single transaction have consistent notion of "current" time, multiple modifications within same transaction bear same time stamp.
it turned out transaction scope problem. controller annotated with:
@transactional(readonly=true) as reads data. reason, transaction scope spans future requests :( i'm little bit surprised here. expect either having transaction each request or not having @ (auto commit transaction).
i have changed
current_date with
date_trunc('day', clock_timestamp()) and runs fine via hibernate.
i guess must read more hibernate , jpa 2 or run similar problems if don't understand transaction scope clearly.
Comments
Post a Comment