0

My requirement is - based on the value of a flag(say for eg. skipDbUpdate) in properties file, I want to save/update the entities or skip these entities in the ongoing transaction. I have implemented a listener on the entity which will throw an exception if this flag is true, but now I have to enhance this behaviour to not throw an error but skip the update of the entity. I tried the below options :

  1. @Immmutable annotation on the entity - Im not able to make this flag based, application uses using spring but I'm not able to combine @ConditionalOnProperty annotation with @Entity and @Immutatble annotations.

@Entity @Immutatble public class EntityA {}

  1. using updateable=false, insertable=false on each field of the entity inside @Column annotation - again, Im not able to make this flag based (same reason as above).

    @Column(insertable = false, updatable = false)

  2. Calling entityManager.detach(o) method inside the Listener when flag is true, as suggested in this question - How to make an Entity read-only? But this is trying to save the entities from other transaction and throwing the error - " java.lang.RuntimeException: org.hibernate.HibernateException: Found two representations of same collection:"

As this is old code base which uses spring only, I cannot use annotations that easily.

Please suggest which is the best possible way to fulfill this requirement?

Thanks

user2340345
  • 793
  • 4
  • 16
  • 38

1 Answers1

0

Lets assume you are using CRUDRepository you can extend the interface and override the save method & in this method you can check foe the flag if skipDb then do nothing else call super.save(….)

Ravik
  • 694
  • 7
  • 12
  • This approach lets me handle the cases where the save() or saveOrUpdate() method is called but when this entity is used in other entities via a Cascading relation, it saves them as is. This is the issue now. – user2340345 Oct 13 '21 at 07:10
  • In that case if possible remove the cascading and handle it in the service method by calling save/update separately – Ravik Oct 14 '21 at 03:31
  • Those entitees are used across the project in multiple places, so it would be huge change. That is why I'm looking for options where I can manage it at the entity level only. – user2340345 Oct 17 '21 at 12:09