JPA / Hibernate Query returns results

I use the EXTENDED persistent context because it will allow me to lazily load the relations of one object on an object, and it will not require SELECT before merging an object with a constant context.

I have a DummyObject with:

  • Last Updated Date Field

  • One-to-Many Relationships

This object is updated every 5 seconds in one JVM with a call em.merge(DummyObject).

In another JVM, I request for a DummyObject making a call as shown below

em.createQuery("from DummyObject").getResultList();  

I also execute this request every 5 seconds.

The problem is that the objects resulting from the query have a timestamp for the very first query after successive calls, although Hibernate generates the correct SQL statement (when I turn on logging) and the database receives updates correctly (I checked).

I also tried all kinds of optimistic locks using @Version to no avail. (See Comments)

Another thing is that it works correctly when:

  • I change PersistentContextType to TRANSACTIONAL (something that won't let me lazily load ONE-MANY)

  • I call the EntityManager.clear () call before I make the request above (Something that also won't allow me to lazily load ONE MANY relationships).

? .

- ? -, query.setHint(,)?

, < EXTENDED " TRANSACTIONAL.

+3
3

. , JVM (A) JVM (B), B , . / . JVM, . , "DummyObject", , , . .

+1

() ?

0

. , . - , .

, JPA . , . .

A method PersistanceContextis available in the class refresh()that captures changes from the database and accordingly updates the instance of the object. But I see how this may not be applicable in a real implementation. So my answer is: you probably can't get it to work.

0
source

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


All Articles