本文介绍了每个子类继承关系的表:如何在不加载任何子类的情况下查询父类???(休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个每个子类继承关系的表,可以描述如下(来自wikibooks.org - 参见这里)

Suppose a Table per subclass inheritance relationship which can be described bellow (From wikibooks.org - see here)

注意父类不是抽象的

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Project {

    @Id
    private long id;

    // Other properties

}

@Entity
@Table(name="LARGEPROJECT")
public class LargeProject extends Project {

    private BigDecimal budget;

}

@Entity
@Table(name="SMALLPROJECT")
public class SmallProject extends Project {

}

我有一个场景,其中我只需要检索父类.由于性能问题,我应该怎么做运行 HQL 查询以检索父类和只是父类而不加载任何子类???

I have a scenario where i just need to retrieve the Parent class. Because of performance issues, What should i do to run a HQL query in order to retrieve the Parent class and just the Parent class without loading any subclass ???

推荐答案

一种解决方法描述如下:

A workaround is described below:

将您的 Parent 类定义为 MappedSuperClass.假设父类映射到 PARENT_TABLE

Define your Parent class as MappedSuperClass. Let's suppose the parent class is mapped To PARENT_TABLE

@MappedSuperClass
public abstract class AbstractParent implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column(table="PARENT_TABLE")
    private String someProperty;

    // getter's and setter's

}

对于每个子类,扩展AbstractParent 类并定义其SecondaryTable.父类中定义的任何持久字段都将映射到SecondaryTable 定义的表.最后,如果需要,使用 AttributeOverrides

For each subclass, extend the AbstractParent class and define its SecondaryTable. Any persistent field defined in the parent class will be mapped to the table defined by SecondaryTable. And finally, use AttributeOverrides if needed

@Entity
@SecondaryTable("PARENT_TABLE")
public class Child extends AbstractParent {

    private String childField;

    public String getChildProperty() {
        return childField;
    }

}

并定义另一个实体,目的是仅检索父类

And define another Entity with the purpose of retrieving just the parent class

@Entity
@Table(name="PARENT_TABLE")
@AttributeOverrides({
    @AttributeOverride(name="someProperty", column=@Column(name="someProperty"))
})
public class Parent extends AbstractParent {}

没有别的.如上所示,我只使用了 JPA 特定的注释

Nothing else. See as shown above i have used just JPA specific annotations

这篇关于每个子类继承关系的表:如何在不加载任何子类的情况下查询父类???(休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:04