在 Xml 映射中使用 Hibernate 设置创建和更新时间

发布于 2024-08-29 00:01:54 字数 317 浏览 10 评论 0原文

我正在将 Hibernate 与 Xml 映射结合使用。我有一个实体,它有两个 timestamp 类型的字段 creationDateupdateDate,当实体创建时,必须用当前 UTC 时间填充这两个字段。坚持并更新。 我知道 @PrePersist@PreUpdate 注释的存在,但我不知道如何在我的 Xml 映射中使用它们的等效项。

再次,我想知道 Hibernate 是否以某种方式本地支持更新和创建时间集。

谢谢

I'm using Hibernate with Xml mappings. I have an entity that has two fields creationDate and updateDate of type timestamp, that have to be filled with the current UTC time when the entity is persisted and updated.
I know about the existence of the @PrePersist and @PreUpdate annotations, but i don't know how to use their equivalent in my Xml mappings.

Again, i was wondering if Hibernate somehow supports natively the update and creation time set.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野却迷人 2024-09-05 00:01:54

我知道 @PrePersist@PreUpdate 注释的存在,但我不知道如何在我的 Xml 映射中使用它们的等效项。

Hibernate3 事件架构提供类似的东西,你可以注册 PreInsertEventPreUpdateEventSaveOrUpdateEvent 的监听器(请参阅 org.hibernate.event 包获取完整列表)设置和更新创建/更新日期。

另一种方法是使用 拦截器< /a>,Session 范围或 SessionFactory 范围,并在 中设置 createDateupdateDate >onSave(...),更新 onFlushDirty(...) 中的 updateDate


更新:我在下面留下了我最初的建议,但我认为正确的方法(应该是我最初的答案)是使用拦截器或事件架构。

您可以使用 timestamp 获取 creationDate updateDate 分别由数据库在插入时以及插入和更新时生成:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="createDate" generated="insert" ... />
  <timestamp name="updateDate" generated="always" ... />
  ...
</class>

请参阅 生成的属性了解完整详细信息。

选项 1

看来 timestamp 不支持 generatead 所以我的建议不起作用。尽管如此,在更仔细地阅读文档后,我的理解是 timestamp 是版本控制的替代方案,我认为它不是像 这样的字段的合适选择createDateupdateDate (它可能适用于后者,但这不是 timestamp 的用途) 。

所以我实际上仍然会使用 生成的属性< /a> 但是简单属性而不是时间戳

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  <property name="updateDate" update="false" insert="false" generated="always" ... />
  ...
</class>

在数据库级别,这需要对updateDate列使用触发器。对于createDate列,使用current_timestamp之类的东西作为默认值会很好地工作。但可能不需要触发器...

选项 2

为了避免触发选项 1,一种变体是使用 updateDate 进行版本控制(从而将其映射为 < code>timestamp):

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" ... />
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  ...
</class>

createDate 的选项 1 相同,在数据库级别使用默认值。

选项 3

请参阅此答案的顶部...

I know about the existence of the @PrePersist and @PreUpdate annotations, but i don't know how to use their equivalent in my Xml mappings.

The Hibernate3 event architecture provides something equivalent and you could register listeners for PreInsertEvent, PreUpdateEvent or SaveOrUpdateEvent (see the org.hibernate.event package for a full list) to set and update the create/update dates.

Another approach would be to use an interceptor, either Session-scoped or SessionFactory-scoped and to set both createDate and updateDate in onSave(...), update the updateDate in onFlushDirty(...).


Update: I'm leaving my original suggestions below but I think that the right approach (should have been my initial answer) is to use an interceptor or the event architecture.

You could use the generated attribute of the timestamp to get creationDate and updateDate generated by the database on insert and on insert and update respectively:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="createDate" generated="insert" ... />
  <timestamp name="updateDate" generated="always" ... />
  ...
</class>

Refer to the section on generated properties for full details.

Option 1

It appears that timestamp doesn't support generatead so my suggestion won't work. Nevertheless, having read the documentation more carefully, my understanding is that timestamp is an alternative to versioning and I don't think that it's an appropriate choice for fields like createDate and updateDate (it may work for the later but that's not what timestamp is for).

So I would actually still use generated properties but with simple properties instead of timestamp:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  <property name="updateDate" update="false" insert="false" generated="always" ... />
  ...
</class>

At the database level, this would require using a trigger for updateDate column. For the createDate column, using something like current_timestamp as default value would work nicely. But triggers are maybe not wanted...

Option 2

To avoid the trigger of the Option 1, a variation would be to use updateDate for versioning (and thus map it as timestamp):

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" ... />
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  ...
</class>

Same approach as Option 1 for createDate, use a default value at the database level.

Option 3

See the top of this answer...

叹倦 2024-09-05 00:01:54

当实体更改时,Hibernate 中的时间戳显然总是自动更新,因此您不能使用 映射创建日期。但是,您可以将其存储为简单的 java.util.Date 属性,并使用 new Date() 对其进行初始化。

对于更新时间戳,请尝试以下操作:

public class MyEntity {
  ...
  private Date updateDate;
  ...
}

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" access="field" column="UPDATE_DATE"/>
  ...
</class>

请注意,timestamp 必须位于映射中的 id 之后。

仅供参考,这里是 的参考时间戳属性

Timestamps in Hibernate are apparently always updated automatically when the entity changes, so you can't use a <timestamp> mapping for the creation date. However, you can store it as a simple java.util.Date property, initialized it with new Date().

For the update timestamp, try this:

public class MyEntity {
  ...
  private Date updateDate;
  ...
}

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" access="field" column="UPDATE_DATE"/>
  ...
</class>

Note that timestamp must come right after id in the mapping.

FYI here is a reference of the timestamp attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文