0

The following are various methods to prevent eager loading of @OneToOne relationships

  1. Use @MapsId as described here - Tried this but didn't work
  2. Fake a @One-Many relationaship but this could lead to issues with complicated JPQL queries.
  3. @OneToOne(fetch=FetchType.LAZY, optional=false) - I don't have non-nullable items
  4. Buildtime Byte Code Instrumentation - Again, I don't have non-nullable one-one relationships and hence will again not work in my case.

As a result, I think only option1 and option2 are suitable ones in my case. I know this topic has been discussed well like here in this excellent discussion. But those answers are almost 5-8 years back and was wondering if there are new methods now.

I have the following specific questions

  1. What are the situations under which @MapsId wouldn't work ?
  2. Are there any other issues with faking a @One-Many relationship as mentioned in option 2 above
  3. Are there any new alternatives in the new versions of Hibernate ?
Vlad Mihalcea
  • 103,297
  • 39
  • 432
  • 788
HopeKing
  • 2,574
  • 3
  • 34
  • 54
  • Why would nullable one-to-one relationships be an issue with byte code instrumentation? With non-nullable relationships, you don't need instrumentation, because Hibernate can always use a proxy instead of an actual entity, knowing it will never have to be replaced with `null` – crizzis May 27 '17 at 15:26
  • Need to clarify: I have relationships that could be null. So sometimes null and sometimes not-null. – HopeKing May 27 '17 at 15:34
  • 1
    I understand that. Let me rephrase: were your one-to-one relationship non-nullable, solution #3 would be the standard way of achieving lazy fetching (it would work out of the box). The other solutions are *all about finding a workaround* for nullable one-to-one relationships. I'm just saying this includes #4, which I think is the least hacky solution to your problem. – crizzis May 27 '17 at 15:54
  • Well, you were right ! I added a foreign key to the owner table and it worked. I haven't listed that as an option above, but seems to work fine. Thanks for your time. – HopeKing May 28 '17 at 10:35

0 Answers0