Hibernate 5.2.18 cannot get a lazy OneToOne anymore

I’ve been reading a number of things around the web on this, so it’s not a completely uninformed question here. According to @vlad in a number of places, in order to get a lazy OneToOne to work, we can either

  1. Activate Lazy Loading Bytecode Enhancement or
  2. Get rid of the parent @OneToOne and use @MapsId on the child.

Unfortunately, for our application we’re coming from Hibernate 4, and due to amount of disturbance it would cause for us, we can’t really do #2. This leaves #1, but we do not use Gradle nor do we use Maven, we use Bazel. This was not an issue in Hibernate 4 for us, due to the trick with optional=false…but now we’re kind of stuck.

Is there any way to enable bytecode enhancement at run time? I tried these settings I see in the docs for 5.3 but to no avail:

Thanks for any help you can lend!


I think this was fixed in 5.4, so maybe you could try to update to the latest version?

@beikov when you say “fixed” what exactly does that mean? What part? We also don’t use entity manager, and the flags in 5.2 appear to only matter if you’re using that.

By fixed I mean that 5.4 does not require eager joining for all one-to-one associations anymore.

Thanks @beikov. Do you have any specific documentation I could look at for the correct way to setup the parent and child side of onetoone in 5.4?

There is only an owning side (where the FK column is) and an inverse side, not sure what you mean by parent/child. There is not much you can do wrong here, just map it as lazy and use mappedBy on the inverse side:

class Entity1 {
  @OneToOne(fetch = LAZY)
  @JoinColumn(name = "...")
  Entity1 e1;

class Entity2 {
  @OneToOne(fetch = LAZY, mappedBy = "entity2")
  Entity1 e1;

Thanks @beikov let me give this a whirl. What I meant about parent/child is that in your example the “owning side” (where the FK column is) would be the child.

Hopefully this works! Thanks.


You’re the man @beikov. Indeed if you use LAZY fetch type and optional false, onetoone is lazy!

1 Like