10

我正在使用带有 Spring 的 Hibernate。

我有一个像这样的模型类。


@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-associations 中。但是 @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 回答