休眠ManyToOne与FetchType.LAZY不提取懒惰 [英] Hibernate ManyToOne with FetchType.LAZY not fetching lazy

查看:110
本文介绍了休眠ManyToOne与FetchType.LAZY不提取懒惰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring中使用Hibernate。

I am using Hibernate with spring.

我有这样的模型类。

I have a model-class like this.


@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已经加载之后急切地加载。

It works in general, but the Category is not loaded lazy, but eagerly after the ForumEntry has been loaded.


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,ForumCategory会在ForumTopic之后加载。

Altough the getter was not called the ForumCategory is loaded right after ForumTopic.

这个问题出现在我的所有@ ManyToOne关联中。然而@OneToMany关联是懒惰地加载的。

This problems appears in all my @ManyToOne-associations. However @OneToMany associating are loaded lazily.

我正在使用maven2进行构建。这些是我的依赖。

I am using maven2 for the build. These are my dependencies.

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

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

Can someone please help me understand what is happening?

推荐答案

我想这是由事实你的类声明为 final ,因此Hibernate不能为它们生成延迟代理。尝试从类声明中移除 final 。

I guess it's caused by the fact that your classes are declared as final, therefore Hibernate cannot generate lazy proxies for them. Try to remove final from class declarations.

这篇关于休眠ManyToOne与FetchType.LAZY不提取懒惰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆