当前位置: 首页 > 面试题库 >

使用FetchType.LAZY休眠ManyToOne不会延迟

夏侯航
2023-03-14
问题内容

我在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;
    }
}

它通常可以正常工作,但是Category不会延迟加载,而是在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称为ForumCategory是在ForumTopic之后立即加载的。

这个问题出现在我所有的@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从类声明中删除。



 类似资料:
  • 问题内容: 我有两个表,并且在数据库中。在表中,我有一个Integer 对该表的引用。(idA是B的外键)当我使用hibernate- tools进行反向工程时,会生成两个Java对象。 和 但是我想要 我怎样才能做到这一点? 谢谢, 问题答案: 因此,我找到了一个解决方案:创建一个自定义并将方法的返回更改为。

  • 我正在使用JPA2.1(Eclipselink2.5.1)和JBoss7.1。 为什么不工作是懒惰加载? 谢谢

  • 问题内容: 我遇到了一个问题,JPA试图在我不想要的时候延迟加载我的数据。从本质上讲,正在发生的事情是我正在使用Service检索一些数据,并且当我将这些数据解析为JSON时,JSON库正在触发hibernate模式以尝试懒惰地加载数据。有什么办法可以阻止这种情况?我在下面给出一个例子。 是否可以将JPA /hibernate设置为不尝试并延迟加载数据? 更新: 我意识到您可以使用FetchTyp

  • 问题内容: 我有一个Spring和Hibernate3在生产中运行良好的应用程序。以下是Spring的applicationContext.xml中会话工厂的配置 生产正常。 现在,对于另一个项目,我们正在迁移到Hibernate4。我们使用org.springframework.orm.hibernate4。*包中的Hibernate 4的SessionFactory,TransacionMan

  • 问题内容: 我正在寻找展示如何将MongoDB与Hibernate集成的资源(最好是在Spring内),以便我可以在RDBMS和NoSql替代品之间进行切换:有人有这样做的经验吗? 问题答案: 您不能轻易做到这一点。Hibernate的重点是将Java对象映射到关系数据库。尽管Hibernate提取了许多细节,但您仍然需要了解关系数据库如何与诸如外键和主键之类的东西一起工作,以及运行查询对性能的影

  • 问题内容: 我的多对一映射存在性能问题。当我在日志文件中调试SQL查询时,可以进行主体查询,但是在我有其他表示多对一对象映射的查询之后。 Entity.hbm.xml: Object1.hbm.xml: Object2.hbm.xml: 查询HBM: 在pom.xml中 问题答案: 您是否尝试过像这样的FetchMode.SELECT?