<legend id='3zcAG'><style id='3zcAG'><dir id='3zcAG'><q id='3zcAG'></q></dir></style></legend>
    • <bdo id='3zcAG'></bdo><ul id='3zcAG'></ul>
  • <i id='3zcAG'><tr id='3zcAG'><dt id='3zcAG'><q id='3zcAG'><span id='3zcAG'><b id='3zcAG'><form id='3zcAG'><ins id='3zcAG'></ins><ul id='3zcAG'></ul><sub id='3zcAG'></sub></form><legend id='3zcAG'></legend><bdo id='3zcAG'><pre id='3zcAG'><center id='3zcAG'></center></pre></bdo></b><th id='3zcAG'></th></span></q></dt></tr></i><div id='3zcAG'><tfoot id='3zcAG'></tfoot><dl id='3zcAG'><fieldset id='3zcAG'></fieldset></dl></div>

      <tfoot id='3zcAG'></tfoot>

      <small id='3zcAG'></small><noframes id='3zcAG'>

        Hibernate 和 MySQL 的创建时间戳和上次更新时间戳

        时间:2023-07-27

          1. <small id='UZlg6'></small><noframes id='UZlg6'>

            <tfoot id='UZlg6'></tfoot>
              <i id='UZlg6'><tr id='UZlg6'><dt id='UZlg6'><q id='UZlg6'><span id='UZlg6'><b id='UZlg6'><form id='UZlg6'><ins id='UZlg6'></ins><ul id='UZlg6'></ul><sub id='UZlg6'></sub></form><legend id='UZlg6'></legend><bdo id='UZlg6'><pre id='UZlg6'><center id='UZlg6'></center></pre></bdo></b><th id='UZlg6'></th></span></q></dt></tr></i><div id='UZlg6'><tfoot id='UZlg6'></tfoot><dl id='UZlg6'><fieldset id='UZlg6'></fieldset></dl></div>
              • <legend id='UZlg6'><style id='UZlg6'><dir id='UZlg6'><q id='UZlg6'></q></dir></style></legend>
                  <tbody id='UZlg6'></tbody>
                  <bdo id='UZlg6'></bdo><ul id='UZlg6'></ul>
                • 本文介绍了Hibernate 和 MySQL 的创建时间戳和上次更新时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  对于某个 Hibernate 实体,我们需要存储其创建时间和上次更新时间.你会如何设计这个?

                  For a certain Hibernate entity we have a requirement to store its creation time and the last time it was updated. How would you design this?

                  • 您将在数据库中使用哪些数据类型(假设 MySQL,可能与 JVM 在不同的时区)?数据类型是否可以识别时区?

                  • What data types would you use in the database (assuming MySQL, possibly in a different timezone that the JVM)? Will the data types be timezone-aware?

                  你会在 Java 中使用哪些数据类型(DateCalendarlong、...)?

                  What data types would you use in Java (Date, Calendar, long, ...)?

                  您会让谁负责设置时间戳——数据库、ORM 框架(Hibernate)或应用程序程序员?

                  Whom would you make responsible for setting the timestamps—the database, the ORM framework (Hibernate), or the application programmer?

                  您会为映射使用哪些注释(例如 @Temporal)?

                  What annotations would you use for the mapping (e.g. @Temporal)?

                  我不仅在寻找可行的解决方案,而且还在寻找安全且设计良好的解决方案.

                  I'm not only looking for a working solution, but for a safe and well-designed solution.

                  推荐答案

                  如果你使用的是 JPA 注解,你可以使用 @PrePersist@PreUpdate 事件钩子来做这个:

                  If you are using the JPA annotations, you can use @PrePersist and @PreUpdate event hooks do this:

                  @Entity
                  @Table(name = "entities")    
                  public class Entity {
                    ...
                  
                    private Date created;
                    private Date updated;
                  
                    @PrePersist
                    protected void onCreate() {
                      created = new Date();
                    }
                  
                    @PreUpdate
                    protected void onUpdate() {
                      updated = new Date();
                    }
                  }
                  

                  或者您可以在类上使用 @EntityListener 注释并将事件代码放在外部类中.

                  or you can use the @EntityListener annotation on the class and place the event code in an external class.

                  这篇关于Hibernate 和 MySQL 的创建时间戳和上次更新时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将时间戳(以毫秒为单位)转换为 Java 中的字符串格 下一篇:Java 日期截止时间信息

                  相关文章

                  最新文章

                    <bdo id='HGU75'></bdo><ul id='HGU75'></ul>

                    <legend id='HGU75'><style id='HGU75'><dir id='HGU75'><q id='HGU75'></q></dir></style></legend><tfoot id='HGU75'></tfoot>

                  1. <small id='HGU75'></small><noframes id='HGU75'>

                  2. <i id='HGU75'><tr id='HGU75'><dt id='HGU75'><q id='HGU75'><span id='HGU75'><b id='HGU75'><form id='HGU75'><ins id='HGU75'></ins><ul id='HGU75'></ul><sub id='HGU75'></sub></form><legend id='HGU75'></legend><bdo id='HGU75'><pre id='HGU75'><center id='HGU75'></center></pre></bdo></b><th id='HGU75'></th></span></q></dt></tr></i><div id='HGU75'><tfoot id='HGU75'></tfoot><dl id='HGU75'><fieldset id='HGU75'></fieldset></dl></div>