1

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.

user1675642
  • 717
  • 1
  • 5
  • 13

1 Answers1

1

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.

Community
  • 1
  • 1
Dragan Bozanovic
  • 21,631
  • 4
  • 36
  • 100