@ManyToOne relationships without compound relationships work very well:
@Entity @Table public class Telefoni{ ... other fields not included for simplicity sake private DjecaRoditelja djecaRoditelja; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "IDDjeteta", referencedColumnName = "IDDjeteta") public DjecaRoditelja getDjecaRoditelja() { return this.djecaRoditelja; } public void setDjecaRoditelja(DjecaRoditelja djecaRoditelja) { this.djecaRoditelja = djecaRoditelja; } } ...
Executes 1 SQL query, retrieves only one row from the Telefoni table and lasts about 10 ms.
But lazy fetching stops working when adding any @ManyToOne relationship with @JoinColumns compound:
@Entity @Table public class Telefoni{ ... other fields not included for simplicity sake private KontaktOsobe kontaktOsobe; @ManyToOne(fetch = FetchType.LAZY) @JoinColumns({ @JoinColumn(name = "KORISNIK", referencedColumnName = "korisnik"), @JoinColumn(name = "PARTNER", referencedColumnName = "Partner"), @JoinColumn(name = "SifraKonOs", referencedColumnName = "SifraOsobe") }) public KontaktOsobe getKontaktOsobe() { return this.kontaktOsobe; } public void setKontaktOsobe(KontaktOsobe kontaktOsobe) { this.kontaktOsobe = kontaktOsobe; } } ...
This executes 37 SQL queries that each parent and each parent will readily retrieve until all relationships are resolved. It lasts about 600 ms.
I get 60 times worse performance for adding one extra relationship to my entity ... Besides changing my database model, is there any way to get lazy choices for working with compound relationships?
EDIT:
After studying this in more detail, the problem is not related to compound relationships, but to relationships through everything that is not connected to the main JPA / Hibernate key.
Therefore, if I have a table with an identity column and a natural unique one, as well as various tables that are related to each other, I have to decide what would be less disastrous if it were eagerly delivered and the other as the Hibernate Primary Key.
source share