Using Custom Cascades in Hibernate / JPA

my problem is quite simple, but very important overall - I think. I conducted the study for several hours, but did not find anything useful.

Introduction:
I have an entity called Employee - this is the central class of my system (say, personnel management). By writing a central one, I mean that he has a lot of relationships with other people. It has the properties of OneToOne, OneToMany, and ManyToOne. Problem:
In some cases, I have to update this object (it also has basic properties, such as "String name;", etc.) - after it has been changed in the same EntityManager context (hibernation session) and should be updated in friend. Of course I can use for this:

entityManager.refreash(employee); 

but ... it will also be updated as a "hundred" related (mentioned: 1-1, -1, 1- relationships) because these relationships have the Cascade.All or Cascade.REFREASH annotation parameter for them. What is not needed in this situation - I use only the Employee object itself in this context. And they DO NOT WANT, because it will hit performance and even locally takes 0.5 s ... Question:
Is there a way to update an object, possibly by disabling cascades by its properties. Or perhaps: is there a way to update only certain properties of an object using Hibernate / JPA ??

I really don't want these 20 or longer SQL queries raised by Hibernate when I update my object in these situations, but in other situations I need it.

In other words, I would say that I need an option like respectCascadesOnRefresh = true / false, which can be set before refreash.

Any other ideas?

Any help is appreciated,

Pedro

+4
source share
1 answer
  • First, if you have only two or three fields to update, use api criteria.

  • Secondly, Cascade is not very good in an absolute context - you better solve some of the dependencies manually.

  • Third, use FetchType.LAZY for collections!

PS AND NO, you cannot use the cascade manually, as these annotations are used once when creating your db schema.

+1
source

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


All Articles