One-to-one Hibernate mapping not proxied if optional

Hibernate @OneToOne performance issue:

Very recently I ran into a performance issue when loading a rather large list of domain objects in Hibernate.

When I turned on the Hibernate show_sql I found that I was making a ton of queries to a related entity that I thought should have been proxied.

Here’s basically what my mapping looked like:

public class LoadingEntity  {

        @OneToOne(mappedBy="loadingEntity", fetch=FetchType.LAZY)
	private RelatedEntity relatedEntity;


public class RelatedEntity  {

	private LoadingEntity loadingEntity;


I had set FetchType.LAZY so why wasn’t it working?

Reason why it’s not proxied:

  • Essentially Hibernate doesn’t proxy the RelatedEntity because it might be null.
    • If it’s null it can’t proxy it.
    • As a result it needs to query the table anyway to determine if it’s null.
    • The folks at Hibernate decided since it’s doing the query anyway it might as well load the object.


  • The links above talk about some solutions for the issue. On a high level:
    • use @OneToMany and deal the list (ie. use get(0)) (Watch out for complex HQL queries where this approach can be problematic)
    • use build time bytecode instrumentation
    • use runtime bytecode instrumentation (limitations exist)
    • use FieldHandled or InterceptFieldEnabled interfaces
  • Our solution:
    • Essentially, don’t use exposed domain model for our task
      • Our problem was we were loading thousands of the LoadingEntity instances into memory just to display a summary list to a user on a search screen.
      • On that summary we don’t need anything from RelatedEntity and we don’t really need much from the LoadingEntity.
      • Therefore we just exposed a SummaryDTO and used a query to help with this performance issue.

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply