使用FetchType.LAZY进行Hibernate ManyToOne无法获取延迟

我正在使用Hibernate和spring。 我有这样的模型类。
@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;
    }
}

它一般工作,但类不是懒惰加载,但在加载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=?
在没有调用getter的情况下,在ForumTopic之后加载了ForumCategory。 这个问题出现在我的所有@ ManyToOne关联中。但是@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>
有人可以帮我理解发生了什么吗?     
已邀请:
我想这是因为你的类声明为
final
,因此Hibernate无法为它们生成惰性代理。尝试从类声明中删除
final
。     
返回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时使用相同的事务。     

要回复问题请先登录注册