在 Xml 映射中使用 Hibernate 设置创建和更新时间
我正在将 Hibernate 与 Xml 映射结合使用。我有一个实体,它有两个 timestamp
类型的字段 creationDate 和 updateDate,当实体创建时,必须用当前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hibernate3 事件架构提供类似的东西,你可以注册
PreInsertEvent
、PreUpdateEvent
或SaveOrUpdateEvent
的监听器(请参阅org.hibernate.event
包获取完整列表)设置和更新创建/更新日期。另一种方法是使用 拦截器< /a>,
Session
范围或SessionFactory
范围,并在中设置
,更新createDate
和updateDate
>onSave(...)onFlushDirty(...)
中的updateDate
。更新:我在下面留下了我最初的建议,但我认为正确的方法(应该是我最初的答案)是使用拦截器或事件架构。
您可以使用timestamp
获取creationDate
和updateDate
分别由数据库在插入时以及插入和更新时生成:请参阅 生成的属性了解完整详细信息。选项 1
看来
timestamp
不支持generatead
所以我的建议不起作用。尽管如此,在更仔细地阅读文档后,我的理解是timestamp
是版本控制的替代方案,我认为它不是像 这样的字段的合适选择createDate
和updateDate
(它可能适用于后者,但这不是timestamp
的用途) 。所以我实际上仍然会使用 生成的属性< /a> 但是 与 简单属性而不是
时间戳
:在数据库级别,这需要对
updateDate
列使用触发器。对于createDate
列,使用current_timestamp
之类的东西作为默认值会很好地工作。但可能不需要触发器...选项 2
为了避免触发选项 1,一种变体是使用
updateDate
进行版本控制(从而将其映射为 < code>timestamp):与
createDate
的选项 1 相同,在数据库级别使用默认值。选项 3
请参阅此答案的顶部...
The Hibernate3 event architecture provides something equivalent and you could register listeners for
PreInsertEvent
,PreUpdateEvent
orSaveOrUpdateEvent
(see theorg.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 orSessionFactory
-scoped and to set bothcreateDate
andupdateDate
inonSave(...)
, update theupdateDate
inonFlushDirty(...)
.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 thegenerated
attribute of thetimestamp
to getcreationDate
andupdateDate
generated by the database on insert and on insert and update respectively:Refer to the section on generated properties for full details.Option 1
It appears that
timestamp
doesn't supportgeneratead
so my suggestion won't work. Nevertheless, having read the documentation more carefully, my understanding is thattimestamp
is an alternative to versioning and I don't think that it's an appropriate choice for fields likecreateDate
andupdateDate
(it may work for the later but that's not whattimestamp
is for).So I would actually still use generated properties but with simple properties instead of
timestamp
:At the database level, this would require using a trigger for
updateDate
column. For thecreateDate
column, using something likecurrent_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 astimestamp
):Same approach as Option 1 for
createDate
, use a default value at the database level.Option 3
See the top of this answer...
当实体更改时,Hibernate 中的时间戳显然总是自动更新,因此您不能使用
映射创建日期。但是,您可以将其存储为简单的java.util.Date
属性,并使用new Date()
对其进行初始化。对于更新时间戳,请尝试以下操作:
请注意,
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 simplejava.util.Date
property, initialized it withnew Date()
.For the update timestamp, try this:
Note that
timestamp
must come right afterid
in the mapping.FYI here is a reference of the
timestamp
attributes.