Hibernate的@DynamicUpdate是否可以与Blobs一起使用? [英] Does Hibernate's @DynamicUpdate work with Blobs?

查看:185
本文介绍了Hibernate的@DynamicUpdate是否可以与Blobs一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity $ b $我使用Hibernate 4.2.3,并且有类似以下的类: b @DynamicInsert 
@DynamicUpdate
@SelectBeforeUpdate
public class Test {
$ b $ @ @Id
private BigInteger theId;

@Lob
@Basic(fetch = FetchType.LAZY)
@JsonIgnore
私有Blob数据;

@Lob
@Basic(fetch = FetchType.LAZY)
@JsonIgnore
私人Blob otherData;

// Getters and setters ....


}

为更新生成的sql包括 data 列,尽管它没有改变。 (更确切地说,我所做的就是获取对象,分离它,阅读 data 并使用它来生成 otherData ,设置它,然后在会话中调用 saveOrUpdate 。)



任何人都可以解释为什么会发生这种情况吗?此功能是否适用于Blob?我搜索了文档,但没有发现。



PS由于性能方面的原因,我没有使用@DynamicUpdate。我知道从这个角度来看它是值得怀疑的。

解决方案

最安全和最便携的数据库和JPA提供者)实现Lobs的真正延迟加载的方法是创建一个人为的懒惰一对一关联这个方法也适用于其他类型的优化,例如当我将它放在一个新的实体和您移动Lob的新实体之间。

想要启用复杂实体的二级缓存,但实体的几列经常更新。然后,我将这些列提取到单独的非二级可缓存实体。

不过,请记住一般的陷阱特定于一对一关联。基本上,要么将它映射为与 @PrimaryKeyJoinColumn 进行强制性(可选= false )一对一关联或使确定外键在声明惰性关联的实体(表格)中(在这种情况下,Lob被移出的实体)。否则,该协会可能会非常渴望,从而破坏了推出该协会的目的。


I'm using Hibernate 4.2.3 and I have a class similar to the following:

@Entity
@DynamicInsert
@DynamicUpdate
@SelectBeforeUpdate
public class Test {

    @Id
    private BigInteger theId;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @JsonIgnore
    private Blob data;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @JsonIgnore
    private Blob otherData;

    // Getters and setters....


}

The sql that this is generating for an update includes the data column, even though it hasn't changed. (To be precise, what I do is get the object, detach it, read the data and use that to generate otherData, set that and then call saveOrUpdate on the session.)

Can anyone explain why this would happen? Does this functionality work with Blobs? I've searched for documentation but found none.

PS I'm not using @DynamicUpdate for performance reasons. I know that it would be questionable to use it from that standpoint.

解决方案

The safest and most portable (between different databases and JPA providers) way to achieve real lazy loading of Lobs is to create an artificial lazy one-to-one association between the original entity and a new one to which you move the Lob.

This approach is suitable for other kinds of optimizations as well, for example when I want to enable second-level caching of a complex entity, but a few columns of the entity are updated frequently. Then I extract those columns to a separate non-second-level-cacheable entity.

However, keep in mind general pitfalls specific to one-to-one associations. Basically, either map it with a mandatory (optional = false) one-to-one association with @PrimaryKeyJoinColumn or make sure the foreign key is in the entity (table) which declares the lazy association (in this case the original entity from which the Lob is moved out). Otherwise, the association could be effectively eager, thus defeating the purpose of introducing it.

这篇关于Hibernate的@DynamicUpdate是否可以与Blobs一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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