How can I optimally update a single property in an object using Hibernate?

I have an object:

public class Data {
    private long           id;
    private long           referenceCount;
    private Blob           dataCache;

    /* getters and setters for the above */
}

      

The corresponding mapping is:

<hibernate-mapping>
  <class name=Data" table=DATA">

    <id name=id" type="long">
      <column name=DATA_ID" precision="20" scale="0" />
      <generator class="assigned" />
    </id>

    <property name="referenceCount" type="long" generated="always" insert="false" update="false">
      <column name="REFERENCE_COUNT" precision="10" scale="0" not-null="true" />
    </property>
    <property name=dataCache" type="blob">
      <column name="DATA" />
    </property>
  </class>
</hibernate-mapping>

      

The dataCache content can be quite large.

Here's where it gets tricky: referenceCount is set by a trigger called by an object that has no mapping relationship to the Data object. Once since I save this object, I have to update the data object, which may or may not already be loaded by the session - in order to keep the reference count correct. Leaving aside whether this is considered a good idea, I need to ensure that the referenceCount is updated on my local session, but I do NOT want to reload the dataCache frame. I could set a lazy attribute to this property, but I'm not sure if it will act the way I hope it will.

How can I do it? I guess one way I could look is: "How do I remove an object from the session cache without loading that object if it doesn't already exist?"

0


a source to share


2 answers


According to this forum post , the type field java.sql.Blob/Clob

should support lazy loading. Therefore, one would expect that execution Session.refresh()

on a persistent instance Data

would, as the documentation indicates, re-read the state of a given instance from the underlying database, but not load dataCache

.



However, I would recommend you move dataCache

for class separation and map it as a mandatory one-to-one relationship. Make sure you don't have a cascading update from Data

to dataCache

. This way, you can have complete control over what is updated and what is downloaded.

+2


a source


Why not update the referenceCount object in the domain if it would fire a trigger in the DB so that it syncs with the stored value.



0


a source







All Articles