Hibernate正在加载惰性对象,而不会被要求 [英] Hibernate is loading lazy objects without being asked for

查看:41
本文介绍了Hibernate正在加载惰性对象,而不会被要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使传输的数据较小,我为数据库中的文件创建了两个实体.文件头,用于保留有关文件和文件blob的一些常规信息,包括fileId和blob. 通常,我只需要询问一般的文件信息.如果现在我要提供文件头列表,那么休眠也会很不幸地选择文件blob(每个都在一个选择中).所以这是我的例子:

In order to keep transfered data small I created two entities for my files in the database. The fileheader to keep some general informations about the files and the fileblob, including fileId and the blob. Often, I only need to ask for general fileinformations. If I now ask for a list of fileheaders, hibernate unfortunatelly selects the fileblob too (each one in a single select). So here is my example:

从Fileh.class中提取:

Extract from Fileh.class:

  @OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY)
  @org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK })
  private Fileblob fileblob;

从选择中提取:

  Criteria createCriteria = persistentSession.createCriteria(Fileh.class);

  List list = createCriteria.list();

ConsoleLog:

ConsoleLog:

Hibernate: 
    select
        this_.`ID` as ID1_78_0_,
        this_.`CHDATE` as CHDATE2_78_0_,
        this_.`CHUSER` as CHUSER9_78_0_,
        this_.`CRDATE` as CRDATE3_78_0_,
        this_.`CRUSER` as CRUSER10_78_0_,
        this_.`EXTENSION` as EXTENSIO4_78_0_,
        this_.`NAME` as NAME5_78_0_,
        this_.`SIZE` as SIZE6_78_0_,
        this_.`VALID_FROM` as VALID_FR7_78_0_,
        this_.`VALID_TO` as VALID_TO8_78_0_ 
    from
        CORE.`FILEH` this_
Hibernate: 
    select
        fileblob0_.`ID` as ID1_77_0_,
        fileblob0_.`FILEBLOB` as FILEBLOB2_77_0_ 
    from
        CORE.`FILEBLOB` fileblob0_ 
    where
        fileblob0_.`ID`=?
Hibernate: 
    select
        fileblob0_.`ID` as ID1_77_0_,
        fileblob0_.`FILEBLOB` as FILEBLOB2_77_0_ 
    from
        CORE.`FILEBLOB` fileblob0_ 
    where
        fileblob0_.`ID`=? .....(and so on)

我是否对我的假设是错误的,我认为 fetch = FetchType.LAZY 就足够了吗?

Am I wrong with my assumption, that fetch = FetchType.LAZY should be sufficient for my purpose?

在此先感谢您的提示和建议.我只是觉得我在错树上...

Thanks in advance for your hints and suggestions. I just think I´m barking up the wrong tree...

亲切的问候, 文森特

编辑:开始深入研究休眠源代码.在DefaultLoadEventListener.proxyOrLoad(...)中,hibernate决定是否将天气作为代理加载.在我的示例中,传递了以下选项:LoadEventListener.INTERNAL_LOAD_NULLABLE,这将导致访问数据库.只是不明白为什么....

Started to dive into hibernate sourcecode. In DefaultLoadEventListener.proxyOrLoad(...) hibernate decides weather to load as proxy or not. In my example following option is passed: LoadEventListener.INTERNAL_LOAD_NULLABLE which results in accessing the database. Just don´t understand why yet....

Edit2:问题已解决:将optional = false添加到OneToOne注释中:

Problem solved: added optional = false to OneToOne-annotation:

@OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY, optional = false)
  @org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK })
  private Fileblob fileblob;

推荐答案

问题解决了,在OneToOne注释中添加optional = false可以解决问题.

Problem solved, adding optional = false in the OneToOne-annotation does the trick.

查看相似性发布

这篇关于Hibernate正在加载惰性对象,而不会被要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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