如何使用Hibernate以最佳方式更新对象中的单个属性?

时间:2022-10-22 07:22:54

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.

'dataCache'的内容可能非常大。

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.

这就是它变得棘手的地方:referenceCount的值由触发器设置,由与Data实体没有映射关系的对象调用。一旦我保存了这个对象,我就必须刷新一个数据对象 - 它可能已经或可能没有被会话加载 - 以保持引用计数的正确性。抛开这是否被视为一个好主意,我必须确保referenceCount的值在我的本地会话中是最新的,但我不想重新加载dataCache blob。我可以在该属性上设置lazy属性,但我不确定它是否会以我希望的方式运行。

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?"

我该怎么做?我想我可以看到它的一种方式是:“如果没有加载该对象,如何在会话缓存中删除该对象,如果它尚未存在?”

2 个解决方案

#1


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.

根据这篇论坛帖子,java.sql.Blob / Clob类型的字段应该支持延迟加载。因此,人们会期望在持久的Data实例上执行Session.refresh()会像文档指定的那样,从底层数据库重新读取给定实例的状态,但不会加载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.

不过,我建议您将dataCache移动到单独的类并将其映射为强制一对一关系。确保没有从Data到DataCache的级联刷新。这样,您就可以完全控制刷新的内容和加载的内容。

#2


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.

为什么不让对象更新域中的referenceCount,只要它触发数据库上的触发器以使其与持久值保持同步。

#1


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.

根据这篇论坛帖子,java.sql.Blob / Clob类型的字段应该支持延迟加载。因此,人们会期望在持久的Data实例上执行Session.refresh()会像文档指定的那样,从底层数据库重新读取给定实例的状态,但不会加载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.

不过,我建议您将dataCache移动到单独的类并将其映射为强制一对一关系。确保没有从Data到DataCache的级联刷新。这样,您就可以完全控制刷新的内容和加载的内容。

#2


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.

为什么不让对象更新域中的referenceCount,只要它触发数据库上的触发器以使其与持久值保持同步。