Hibernate ManyToOne与FetchType.LAZY不提取懒惰

时间:2011-04-08 11:20:12

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

我在春天使用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关联中。但是@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>

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

有人可以帮我理解发生的事情吗?

2 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分: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时使用相同的事务。