简体   繁体   English

Hibernate FechType.LAZY不适用于复合@ManyToOne关系

[英]Hibernate FechType.LAZY not working for composite @ManyToOne relationships

@ManyToOne relations without composite relations work just fine: 没有复合关系的@ManyToOne关系工作得很好:

@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. 这将执行1个SQL查询,从Telefoni表中只获取一行并持续大约10毫秒。

But lazy fetching stops working when adding any @ManyToOne relationship with composite @JoinColumns: 但是,当与复合@JoinColumns添加任何@ManyToOne关系时,延迟提取会停止工作:

@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. 这将执行37个SQL查询,急切地获取每个父实体和每个父项的父项,直到它解析所有关系。 This lasts for about 600ms. 这持续约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? 我为我的实体添加一个额外的关系,性能提高了60倍......除了更改我的数据库模型之外,还有什么方法可以让懒惰的提取与复合关系一起工作吗?

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. 在对此进行更详细的研究之后,该问题与复合关系无关,而是通过任何未超过JPA / Hibernate定义的主键的关系。

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. 因此,如果我有一个带有标识列和自然唯一的表,以及通过其中一个或多个相关的各种表 - 我必须决定哪个是急于获取的灾难性的,并将另一个作为Hibernate的主键。

On the related table KontaktOsobe, primary key has to be a @EmbeddedId that's comprised of the components forming the relation. 在相关表KontaktOsobe上,主键必须是@EmbeddedId,它由构成关系的组件组成。

@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... 如果同时你也有关系定位另一个字段(即自动增量标识字段),你必须决定哪个延迟提取将起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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