java - Prevent Hibernate session from flushing/ storing invalid dirty entities -


I would like to know that Hibernate 4.3.4 (with Spring and Hibernate vader). In my code, I use the Hibernate Validator (for example, the implementation of a .validate () method manually), which says even before saving an entity. Valid () method gives a list of errors, if someone joins, otherwise the session is asked to update .update () is called to store the unit, after which there is a commitment of transaction.

This works, but when the example is manipulated (the post / request parameters are set in unit) then the unit and related hibernate session are marked 'dirty' and unit next session Is accumulated with. ().

In my case, I would like to have clear control over those institutions which can be stored and prevent any dirty unit from being stored, how do I get it?

Edit:

I know that I am going to clean an entity through a unit (or a merge) Removing it manually can be regulated, but this is not my goal. Rather than manually regulating the continuity, I would like the offset status that an entity that was not clearly saved and where the transaction was not explicitly stored will not be stored in the database (such as through interceptor? ).

If you modify entities and do not want to clearly fly them, You can separate before modifying. Different entities will no longer be maintained by the context firmly.

If you want to separate all the entities and manage some of them, you can first clear the references to your perseverance and then merge the elements you do Required to be managed:

Hibernate API:

  session.clear (); ManagedEntity = session.merge (separate entity);   

JPA:

  entityManager.clear (); ManagedEntity = entityManager.merge (different entity);   

Edit 2 All changes made to managed organizations have been transitioned to Transaction Commitment. I do not know about any facility to be in J.P.A. or Hibernate, which can stop this behavior. Therefore, in addition to isolating some or all organizations, you have a few other options, but none is what you are looking for:

  • Obtaining entities outside a transaction To do so, they are separated hands. It seems that the closest to what you want - there is no problem with the managed changes, only one clear unit will be saved by merging and you will have to deal less firmly with the API. However, you will still need to open a session to merge those entities which you want to save with changes.

  • DTO / POJO (which are always different) in your queries New operator benefits from separating mapping of persistence from application from this perspective it happens. Though a group of new classes may not be worth the start, and it is not fully in the application, conceptual complexity is called.

  • Is working inside the transaction, but rolling back rather than earning. You will not be able to save anything, and it is a rough way to prevent changes from being synchronized with DB. Unfortunately, you have to either do a transaction or rollback.

  • To make deep copies of the institutions, to tell them the truth, it does not matter to me, not only is it adding to the perfection, though the JPA space Although Hibernate and Eclipseelch allow both transactions to be marked or read as a single query as a result - only This can be useful for you, as

    When a continuous object is read only, hibernation does not dirty-check the simple properties

    Changing flushing becomes one becomes a bit more complicated when it comes to relationships or please.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -