rain_drops rain_drops - 1 year ago 86
Java Question

Hibernate very slow with Multiple @OneToOne relationships

I am having a problem with Hibernate. The problem is that some of the queries take too long time to execute.
I found that one of the Entities "Client" has 3 Relationships @OneToOne to another Entity "Address" - for different type of addresses : "managementAddress", "correspondenceAddress" and "buildingAddress". In the Entity of "Address", there is no Relationship to the "Client" entity. I figured out the the slow loading comes from these Relationships.
The @OneToOne relationships are initialized Eagerly. I am not allowed to changed the Model Relationships. Does anyone have idea how could I increase the speed of Execution? Thanks.

This is how the Relationships Are declared in "Client" entity:

private Address managementAddress;

This is a sample from the Hibernate statistics, for loading 50 "Client" entities:

7248515 nanoseconds spent acquiring 51 JDBC connections;
2583681 nanoseconds spent releasing 51 JDBC connections;
43894536 nanoseconds spent preparing 203 JDBC statements;
4106621728 nanoseconds spent executing 203 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;

Answer Source

Mark it will FetchType.LAZY to lazy load addresses only when required.

@OneToOne(optional=true, fetch=FetchType.LAZY)
private Address managementAddress;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download