在Xml映射中使用Hibernate设置创建和更新时间 [英] Set creation and update time with Hibernate in Xml mappings
问题描述
我使用Hibernate和 Xml 映射。我有一个实体,它有两个 creationDate 和 updateDate 类型的 timestamp
,它们必须用current UTC时间,实体持续更新。
我知道 @PrePersist
和 @PreUpdate
注释的存在,但我不知道如何在我的Xml映射中使用它们的等价物。
同样,我想知道Hibernate是否支持本地更新和创建时间集合。
谢谢
我知道
@PrePersist
和@PreUpdate
注解,但我不知道如何在我的Xml映射中使用它们的等价物。
Hibernate3 event architecture 提供了一些相当的东西,你可以为 PreInsertEvent
, PreUpdateEvent
或 SaveOrUpdateEvent
(请参阅 org.hibernate.event
包的完整列表)来设置并更新创建/更新te日期。
另一种方法是使用拦截器, SessionFactory
-scoped并在 onSave()中设置
,更新 createDate
和 updateDate
。 。) onFlushDirty(...)
中的 updateDate
。
更新:我在下面留下原始建议,但我认为正确的方法是我的第一个答案)是使用拦截器或事件架构。
您可以使用 生成的$ 属性rel =noreferrer>
即可获得 timestamp
creationDate
强和
<$ p $数据库生成的
updateDate
p> < class name =MyEntitytable =MY_ENTITY>
< id ... />
< timestamp name =createDategenerated =insert... />
< timestamp name =updateDategenerated =always... />
...
< / class>
请参阅产生的属性以获取全部详细信息。
选项1
看来 timestamp
不支持< a href =http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-generated =noreferrer> generatead
,所以我的建议无效。尽管如此,仔细阅读文档后,我的理解是, timestamp
是版本控制的替代方案,我不认为这是一个合适的选择对于像 createDate
和 updateDate
等字段对于后面的,但这不是什么 timestamp
是)。
所以我实际上仍然会使用生成属性 但是与简单属性而不是 timestamp
:
< class name =MyEntitytable =MY_ENTITY>
< id ... />
< property name =updateDateupdate =falseinsert =falsegenerated =always... />
...
< / class>
在数据库级别,这需要使用 updateDate
列。对于
createDate
列,使用像 current_timestamp
这样的默认值可以很好地工作。但可能不需要触发器......
选项2
为避免触发选项1 ,一个变体是使用 updateDate
进行版本控制(因此将其映射为 timestamp
) :
< class name =MyEntitytable =MY_ENTITY>
< id ... />
< timestamp name =updateDate... />
...
< / class>
对于 createDate
,与选项1相同,使用数据库级别的默认值。
选项3
查看本答案的顶部...
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
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...
这篇关于在Xml映射中使用Hibernate设置创建和更新时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!