简体   繁体   中英

Hibernate FechType.LAZY not working for composite @ManyToOne relationships

@ManyToOne relations without composite relations work just fine:

@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);

This executes 1 SQL query, fetching just one row from Telefoni table and lasts about 10ms.

But lazy fetching stops working when adding any @ManyToOne relationship with composite @JoinColumns:

@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 eagerly fetching every parent entity and every parent's parent until it resolves all relations. This lasts for about 600ms.

I'm getting 60 times worse performance for adding one extra relation to my entity... Other than changing my database model, is there any way to get lazy fetching to work with composite relations?

EDIT:

After investigating this in more detail, the issue is not related to composite relationships, but relationships trough anything that is not over JPA/Hibernate defined primary key.

Hence, if I have a table with an identity column and a natural unique, and various tables that are related trough one or the other - I have to decide which would be less catastrophic to be eagerly fetched and put the other one as Hibernate's primary key.

On the related table KontaktOsobe, primary key has to be a @EmbeddedId that's comprised of the components forming 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 relations targeting another field (ie autoincremental identity field), you have to decide for which lazy fetching will work...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM