1

is there a way in Hibernate 3.6 to only load a child entity if a boolean flag in the parent entity is set to true?

For example:

@Entity
class Parent {
  @OneToOne
  private Child child;
  private boolean loadChild;
}

So if loadChild == false, the child should not be loaded from the db and always be null, otherwise it should be loaded (if available, of course). Currently, the child is loaded eagerly and it would be nice to keep it that way.

Bascially, what we want to prevent here is the actual loading of the child and all the performance impact this may have. It would be easy to load it and then use a getter to return null, but this would have performance impacts by loading a child that is not actually needed.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • What does this have to do with Hibernate? Also, a bit more context would be helpful so that we're not running into the typical X-Y-Problem here – Smutje May 28 '20 at 11:23
  • Well, we're using Hibernate to manage these entities. I'm not sure what else on context I can give here. It's really just about this... – Florian Schaetz May 28 '20 at 11:52
  • Well, an example of how this is used in the code. Also, is the `Child` associated using a `@OneToOne` relationship? Then you could make it `EAGER` loaded etc. – Smutje May 28 '20 at 11:53
  • Yes, it's OneToOne and it's loaded eagerly at the moment and if possible, we would like to keep it that way. – Florian Schaetz May 28 '20 at 11:56
  • So _what exactly_ is the problem? Either it is initialized during loading (because it is EAGER) or it is empty if it is not present. – Smutje May 28 '20 at 11:57
  • That is the current state. But what we want is: Either is is initialized during loading (because eager AND loadChild == true) or is is empty (because either is is not present OR loadChild == false). – Florian Schaetz May 28 '20 at 11:58
  • @FlorianSchaetz, do you intend to use the value of `loadChild` from db? Or do you want to use this flag simply as a transient field in a dirty entity to obtain the managed one? – Minar Mahmud May 28 '20 at 12:41
  • My plan was to use loadChild directly from the db. – Florian Schaetz May 28 '20 at 12:43

1 Answers1

0

I am not sure if JPA and/or Hibernate supports this directly. One workaround can be:

Move down loadChild to Child and then use @Where in Parent.

But, at field level, @Where works only for collection-valued attributes. So you will have to wrap the child with a Collection for @Where to work.

@Where(clause = "load_with_parent = true") // lets say the column name is load_with_parent
@OneToMany
private List<Child> childs;


Further Reading:

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
  • Unfortunately, this is not the child's decision. The child can be contained in dozens of other parents as well and they all have their own thing to say about loading it or not. What I would need is `@WhereParent`, but unfortunately, we only have `@Where` and `@WhereJoinTable` ;-) – Florian Schaetz May 28 '20 at 13:43