Hibernate FechType.LAZY not working for compound links @ManyToOne

@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; } } ... // in some DAO method tel = entityManager.find(Telefoni.class, primaryKey); 

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; } } ... // in some DAO method tel = entityManager.find(Telefoni.class, primaryKey); 

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.

+4
source share
1 answer

In the linked KontaktOsobe table, the primary key must be @EmbeddedId, which consists of the components that make up the relation.

 @Entity @Table public class KontaktOsobe{ private KontaktOsobePK pk; @EmbeddedId public KontaktOsobePK getPk() { return pk; } // ... } @Embeddable public class KontaktOsobePK implements Serializable { private static final long serialVersionUID = 1L; private String sifraOsobe; private String partner; private String korisnik; // getters, setters, equals and hashode methods go here... } 

If at the same time you also have relationships aimed at another field (i.e. an auto-incrementing identity field), you must decide for which lazy sample will work ...

0
source

All Articles