gpt4 book ai didi

java - Hibernate DateCreated,DateModified 列来自 DB 而不是 VM(App)

转载 作者:行者123 更新时间:2023-11-30 11:57:31 24 4
gpt4 key购买 nike

使用 Hibernate 3.5.1 和 MSSQL 2008

您好,您正在尝试实现典型的 DateCreated 和 DateModified 列。我遇到的问题是时间必须从数据库而不是应用程序设置。

对于 DateCreated,我可以将属性设置为 generated = always 并将 MS SQL 2008 上的默认约束(是的,读起来对,该死的 MS)设置为默认值 GetDate()

我将如何处理 DateModified?

最佳答案

For DateCreated I can set the property to generated = always and set a default constraint (yes that reads right damn MS) on MS SQL 2008 to a default value of GetDate()

好吧,您实际上应该使用 generated = insert 作为创建日期,它不应该在后续更新时更改,这将在更新期间为此列保存额外的选择。

How would I go about it for DateModified?

这就是您应该使用 generated = always(以及数据库端的触发器)的地方。

这是引用文档中关于它们的内容:

5.6. Generated properties

Generated properties are properties that have their values generated by the database. Typically, Hibernate applications needed to refresh objects that contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to Hibernate. When Hibernate issues an SQL INSERT or UPDATE for an entity that has defined generated properties, it immediately issues a select afterwards to retrieve the generated values.

Properties marked as generated must additionally be non-insertable and non-updateable. Only versions, timestamps, and simple properties, can be marked as generated.

  • never: (the default): the given property value is not generated within the database.

  • insert: the given property value is generated on insert, but is not regenerated on subsequent updates. Properties like created-date fall into this category. Even though version and timestamp properties can be marked as generated, this option is not available.

  • always: the property value is generated both on insert and on update.

关于java - Hibernate DateCreated,DateModified 列来自 DB 而不是 VM(App),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com