Is there any way to observe changes in the database table through Hibernate?

In my application, I save a small table containing 50 records in a singleton class, because the data in this table is unlikely to change - for example. list of countries.

Although the concept is not very good, I must continue it now. Is there any solution in Hibernate that monitors the change in the table and when it changes, the class method is called to update the variable.

+3
source share
4 answers

Hibernate will not receive notification of changes made at the table level. A (bad) solution would be to update the data via the Hibernate API and use one of the callbacks provided by Interceptor to make some black magic voodoo with Singleton. But honestly, the right way to handle this would be to get rid of this singleton and put the data in the second level cache (and invalidate the cache if you update the table manually).

(EDIT: As mentioned in ChssPly76's comment, there is an interim solution if deleting a singleton is not an option, which is to modify a singleton to search and return to cached hibernated entities, such as your countries.)

+6
source

Interceptor#onFlushDirty(). , . , . , , , , . java.util.TimerTask .

, Singleton - . . , , - Java EE, ServletContext .

+1

L2 cache, . , Hibernate , , /. , , Hibernate , , , @BalusC.

+1
source

It seems that the easiest approach to this is to trigger at the end of the database, and not inside your application. But it depends, of course, on the underlying database, and if you really want to use SQL, if everything else is done by Hibernate.

0
source

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


All Articles