【问题标题】:Hibernate ManyToOne with FetchType.LAZY not fetching lazy使用 FetchType.LAZY 休眠 ManyToOne 不会延迟获取
【发布时间】:2011-08-01 10:42:03
【问题描述】:

我正在使用带有 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 进行构建。这些是我的依赖项。 <pre></pre>

  <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>

有人可以帮我了解发生了什么吗?

【问题讨论】:

    标签: hibernate spring maven-2 lazy-loading many-to-one


    【解决方案1】:

    我猜这是因为你的类被声明为final,因此 Hibernate 无法为它们生成惰性代理。尝试从类声明中删除final

    【讨论】:

    • 谢谢先生,分享您的智慧。
    【解决方案2】:

    看起来事务/会话在返回 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 的地方使用相同的事务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      相关资源
      最近更新 更多