Disable Hibernate auto-update on flash for read-only synonyms

I have a table and two databases that have the same table, but one is a symbolic link for the other, and only reading is allowed in this table.

I mapped the table to Java using Hibernate, and I use spring to set the Entity Manager data source as one of two databases based on some input criteria.

I only call read-only operations (selects) when I am connected to the second database, but it seems that Hibernate is trying to dump something back to the database, and this does not allow me to report that the update is not allowed in this view.

How to disable this update only for the second data source and keep it normal for the first?

Update: Looking at the stack trace, the flash seems to run here:

           at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions (AbstractFlushingEventListener.java:321)
           at org.hibernate.event.def.DefaultFlushEventListener.onFlush (DefaultFlushEventListener.java:50)
           at org.hibernate.impl.SessionImpl.flush (SessionImpl.java:1027)
           at org.hibernate.impl.SessionImpl.managedFlush (SessionImpl.java data65)
           at org.hibernate.ejb.AbstractEntityManagerImpl $ 1.beforeCompletion (AbstractEntityManagerImpl.java:504)
           ... 55 more

Is this related to the hibernate.transaction.flush_before_completion property? Can I set the value to false for the second data source?

+4
source share
2 answers

Most likely, your entities become "dirty" at the very moment when they are loaded from the database, and Hibernate believes that it should store the changes. This happens if your accessors (retrieval and installation methods) do not return the same value or link that Hibernate installed.

In our code, this happened with lists, the developers created new instances of the list because they did not like the type that they received in the installer.

If you do not want to change the code, change the display to access the field.

You can also prevent Hibernate from saving changes by setting FlushMode to the session never, but this only hides the real problem, which will still occur in other situations, and lead to unnecessary updates.

+7
source

First you need to determine if it is DDL or DML. If you do not know, I recommend that you set hibernate.show_sql = true to capture an expression of abuse.

If it is DDL, then most likely it will be Hibernate, updating the scheme for you, and you need to additionally configure the hibernate.hbm2ddl.auto parameter as " update " or " none ", depending on whether you use the actual db or symbolic (only for reading), respectfully. You can use " validate " instead.

If this is DML, I would first determine if your code is for any reason modifying an instance that is still bound to an active Hibernate session. If this is the case, subsequent reading may result in discarding these changes without explicitly storing the object (Grails?). If so, consider eliminating the instance that caused the flash (or using transport objects instead).

Perhaps you are using any aspects or events of the Hibernate life cycle to provide object auditing? It can also lead to read-only access, which will trigger an insert or update.

It may turn out that you need to provide alternative mappings for the intruder class if the field updateability comes into play, but the code does everything exactly as you would like (this is unlikely; 0). If you are in the world of annotations, this can be difficult. If you are working with hbm.xml, it is easier to provide an alternative mapping.

+1
source

All Articles