2

プロバイダーとして Hibernate 4.2.0-Final で JPA 2 を使用しており、次のエンティティがあります。

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

    @Id
    private String id;

    .. Person attributes ..
    .. Getters/Setters ..
}

@Entity
@Table(uniqueConstraints={@UniqueConstraint(name="UniqueCode", columnNames="code")})
public class Customer extends Person {

    @Column(nullable=false)
    private String code;

    .. Other Customer attributes ..
    .. Getters/Setters ..
}

そして、私は次のJPQLを持っています:

SELECT count(distinct c.code) FROM Customer c

どの Hibernate が次の SQL を生成しているか:

select
    count(distinct customer0_.code) as col_0_0_ 
from
    Customer customer0_ 
inner join
    Person customer0_1_ 
        on customer0_.id=customer0_1_.id

しかし、Customer に固有のフィールドである個別のコードで Customers をカウントするだけでよいため、「Person」への内部結合は必要ありません。Hibernate に次のような SQL を生成してもらいたい (つまり、テーブル 'Person' を結合せずに):

select
    count(distinct customer0_.code) as col_0_0_ 
from
    Customer customer0_

不要な内部結合を避けるように Hibernate に指示する方法はありますか? たぶん、Hibernate 固有のクエリ ヒントでしょうか。

4

1 に答える 1

0

では、オブジェクト階層内のクラスごとに個別のテーブルが使用されますJOINED StrategyJPA

したがって、オブジェクトをロードする場合は、親クラスからsubclassも情報ロードする必要があります (サブクラスだけでは、親クラスの属性がなければ全体像を表すことができないため)。これによりJOIN、サブオブジェクトのクエリが実行されます。

JPAのドキュメントから、これが JOINED 戦略の主な欠点であることがわかります。

1 ) 以下で説明する table-per-class 戦略の特定の用途を除けば、joined 戦略は多くの場合、継承モデルの中で最も低速です。サブクラスを取得するには 1 つ以上のデータベース結合が必要であり、サブクラスを格納するには複数の INSERT または UPDATE ステートメントが必要です。

于 2014-08-14T20:36:17.640 に答える