views:

136

answers:

2

I have an object:

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

    /* getters and setters for the above */
}

Corresponding to this is a mapping:

<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 contents of 'dataCache' may be quite large.

Here's where it gets tricky: The value of referenceCount is set by a trigger, invoked by an object that has no mapping relation to the Data entity. As soon as I save this object, I must refresh a 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 viewed as a good idea, I have to ensure that the value of the referenceCount is up to date in my local session, but I do NOT want to reload the dataCache blob. I could set the lazy attribute on that property, but I'm not sure that it'll act the way I hope it will.

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

A: 

Why not have the object update the referenceCount in the domain whenever it would fire the trigger on the DB to keep it synced with the persisted value.

ebrown
+2  A: 

According to this forum post, field of type java.sql.Blob/Clob, should support lazy loading. Therefore, one would expect that doing a Session.refresh() on a persistent instance of Data would, as documentation specifies, re-read the state of the given instance from the underlying database, but not load the dataCache.

Still, I would recommend that you move the dataCache to separate class and map it as mandatory one-to-one relationship. Make sure you don't have cascade refresh from Data to DataCache. That way you can exercise full control over what gets refreshed and what gets loaded.

javashlook