10

春に休止状態を使用しています。

私はこのようなモデルクラスを持っています。


@Entity
@Table(name = "forumtopic")
public final class Forumtopic extends AbstractUserTracking implements
    java.io.Serializable {

/**SNIP **/

    private Forumcategory forumcategory;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FkForumcategoryId", nullable = false)
    public Forumcategory getForumcategory() {
        return this.forumcategory;
    }

    public void setForumcategory(final Forumcategory forumcategory) {
        this.forumcategory = forumcategory;
    }
}

これは一般的に機能しますが、Category は遅延してロードされるのではなく、ForumEntry がロードされた後に熱心にロードされます。

Hibernate: 
    select
        forumtopic0_.PkId as PkId19_0_,
        forumtopic0_.CreateDate as CreateDate19_0_,
        forumtopic0_.FkCreateUserId as FkCreate3_19_0_,
        forumtopic0_.FkLastUserId as FkLastUs4_19_0_,
        forumtopic0_.LastChange as LastChange19_0_,
        forumtopic0_.FkForumcategoryId as FkForum10_19_0_,
        forumtopic0_.PublishCategory as PublishC6_19_0_,
        forumtopic0_.State as State19_0_,
        forumtopic0_.Text as Text19_0_,
        forumtopic0_.Topic as Topic19_0_,
        forumtopic0_.FkTpUserId as FkTpUserId19_0_ 
    from
        forumtopic forumtopic0_ 
    where
        forumtopic0_.PkId=?
Hibernate: 
    select
        forumcateg0_.PkId as PkId17_0_,
        forumcateg0_.CreateDate as CreateDate17_0_,
        forumcateg0_.Name as Name17_0_,
        forumcateg0_.FkRequestId as FkReques4_17_0_,
        forumcateg0_.FkTpUserId as FkTpUserId17_0_ 
    from
        forumcategory forumcateg0_ 
    where
        forumcateg0_.PkId=?

ゲッターは呼び出されませんでしたが、ForumCategory は ForumTopic の直後にロードされます。

この問題は、私のすべての @ManyToOne-association に見られます。ただし、@OneToMany 関連付けは遅延ロードされます。

ビルドにはmaven2を使用しています。これらは私の依存関係です。

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
  </dependency>


  <dependency>
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    <version>3.3.1.GA</version> 
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>ejb3-persistence</artifactId>
    <version>1.0.2.GA</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <type>jar</type>
    <version>3.4.0.GA</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <type>jar</type>
    <version>3.4.0.GA</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search</artifactId>
    <version>3.1.0.GA</version>
</dependency>

誰かが何が起こっているのか理解するのを手伝ってくれますか?

4

2 に答える 2

16

クラスが として宣言されていることが原因だと思いますfinal。そのため、Hibernate はそれらの遅延プロキシを生成できません。finalクラス宣言から削除してみてください。

于 2011-04-08T12:12:25.990 に答える
1

ForumTopic を返した後、トランザクション/セッションが閉じられたようです。したがって、それは分離されたエンティティになります。getForumCategory を実行しようとすると、操作を実行するためのセッションがありません。次のように、同じセッションで ForumCategory をプリフェッチできます。

getForumTopic で、リストを返す前に (getAllForumTopic があると仮定して)

public List<ForumTopic> getAllForumTopic() {
  <snip> 
  List<ForumTopic> topics = <SNIP: get the list of ForumTopic>

  for(ForumTopic topic: topics)
      topic.getForumCategory()

  return topics;
}

これにより、同じセッションで ForumCategory が取得されます。それ以外の場合は、getAllForumTopic の呼び出し関数でトランザクションを作成し、getForumCategory を呼び出す必要がある場所で同じトランザクションを使用する必要があります。

于 2011-04-08T12:00:03.400 に答える