3

私はエンティティクラスを持っています

class B{
    @ManyToOne(fetch = FetchType.EAGER)
    private A a;
    @ManyToOne(fetch = FetchType.LAZY)
    private C c;

}

特定のシナリオでは、オブジェクト A を既に持っているため、オブジェクト A をロードしたくありません。Cについても同じです。しかし、これらは特定のシナリオであり、これらのオブジェクトをまったくロードしたくありません。

エンティティ オブジェクトの特定のプロパティを読み込まないように hibernate に指示する方法はありますか。{Eager/Fetch の提案はありません。これが特定の場合にのみ発生することを望んでいます]

注:現在、オブジェクトを取得するために Criteria を使用しています。

4

1 に答える 1

2

Since you're using HQL for your queries, you can specify your fetching strategy at runtime based on your condition using the "fetch" keyword, like below:

List result = sess.createCriteria(B.class)
    .add( Restrictions.like("name", "yourcondition%") )
    .setFetchMode("a", FetchMode.EAGER)
    .setFetchMode("c", FetchMode.LAZY)
    .list();

Edit:

Since FetchMode.EAGER and FetchMode.LAZY is deprecated use, FetchMode.SELECT or FetchMode.JOIN

List result = sess.createCriteria(B.class)
    .add( Restrictions.like("name", "yourcondition%") )
    .setFetchMode("a", FetchMode.JOIN)
    .setFetchMode("c", FetchMode.SELECT)
    .list();

If you want to avoid the SELECT or JOIN check here.

于 2013-02-05T06:45:48.140 に答える