【问题标题】:@Version or Trigger?@Version 还是触发器?
【发布时间】:2015-04-07 09:15:13
【问题描述】:

我想在所有表中添加一个'LastModification'列,该列将存储相关行的修改日期,我的Web应用程序中使用的框架是:SpringMVC,Hibernate和MySQL数据库。

我的问题是:

  • 新列的类型是:日期时间还是时间戳?根据 我的研究,我没有发现日期时间有更大的范围,但是 未来我可能面临任何风险或限制?
  • 侧面性能,为每个表添加触发器是否更好 将更新“LastModification”列,否则我必须 使用注释“版本?我试图实现版本,但我有一个 很多错误,解决方案需要更改现有注释 实体(即:cascade = CascadeType.ALL 等...),而我没有 更改它的权利:(所以我打算添加触发器,但我不知道是不是 是否会影响我的数据库的性能?

下面的脚本是我打算实现的触发器示例:

CREATE TRIGGER trg_i_table001
BEFORE INSERT ON table001
FOR EACH ROW  SET NEW.lastModification = Now();

CREATE TRIGGER trg_u_table001
BEFORE UPDATE ON table001
FOR EACH ROW   SET NEW.LastModification = Now();

感谢您的回答。

【问题讨论】:

    标签: mysql hibernate spring-mvc triggers


    【解决方案1】:

    您希望提供乐观锁定还是仅在记录被修改时进行记录?

    不要在 MySQL 中使用 @Version 时间戳,因为如果您更新同一记录的速度过快(时间戳值的分辨率较低),它将引发异常。始终使用 int@Version

    如果您只有一个应用程序写入数据库,您可以将逻辑保留在应用程序中。如果有多个应用程序,请使用 db(触发器)。

    如果您可以使用 Spring Data JPA 审计功能,您可能会节省大量时间:

    http://docs.spring.io/spring-data/jpa/docs/1.8.0.RELEASE/reference/html/#auditing

    这使您可以将@LastModifiedDate 注释添加到 SDJ 将自动为您更新的字段。如果用户已登录,您还可以跟踪他们是谁。

    【讨论】:

      【解决方案2】:

      您可以使用 Spring Data JPA,Spring 使在您的字段上使用注释 @CreatedBy, @CreatedDate, @LastModifiedBy, @LastModifiedDate 变得如此简单。您可以按照以下简单示例进行操作

      // Will need to enable JPA Auditing
      @Configuration
      @EnableJpaAuditing(auditorAwareRef = "auditorAware")
      class JpaConfig {
          // Creating a bean of AuditorAwareImpl which will provide currently logged in user
          @Bean
          public AuditorAware<String> auditorAware() {
              return new AuditorAwareImpl();
          }
      }
      
      // Moving necessary fields to super class and providing AuditingEntityListener entity listner class
      @MappedSuperclass
      @EntityListeners(AuditingEntityListener.class)
      abstract class Auditable<U> {
      
          @CreatedBy
          protected U createdBy;
      
          @CreatedDate
          @Temporal(TIMESTAMP)
          protected Date createdDate;
      
          @LastModifiedBy
          protected U lastModifiedBy;
      
          @LastModifiedDate
          @Temporal(TIMESTAMP)
          protected Date lastModifiedDate;
      
          // Getters and Setters
      }
      
      // Creating implementation of AuditorAware and override its methods to provide currently logged in user
      class AuditorAwareImpl implements AuditorAware<String> {
      
          @Override
          public String getCurrentAuditor() {
              return "Naresh";
              // Can use Spring Security to return currently logged in user
              // return ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()
          }
      }
      
      @Entity
      class File extends Auditable<String> {
          @Id
          @GeneratedValue
          private Integer id;
          private String name;
          private String content;
      
          // Getters and Setters
      } 
      

      您可以阅读Spring Data JPA Auditing: Saving CreatedBy, CreatedDate, LastModifiedBy, LastModifiedDate automatically了解更多详情

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 2019-07-26
        • 2012-11-18
        • 1970-01-01
        • 2012-03-12
        • 2016-10-03
        • 1970-01-01
        • 2019-12-16
        • 2011-12-29
        相关资源
        最近更新 更多