在 spring + hibernate 中保存 creationTimestamp 和 updatedTime

Save creationTimestamp and updatedTime in spring + hibernate

我需要使用 createdDate 和 updatedDate 更新 postgres 数据库 我尝试使用方法 1,但它插入的是空值。 当我阅读时,@prepersist 注释似乎不适用于会话。

所以我决定采用方法 2:Hibernate @CreationTimeStamp 注解,我添加了 hibernate-annotations maven 依赖项,但 @CreationTimeStamp 未解析并给出编译错误。

有人可以告诉我如何解决这个问题吗?

方法一 用@Entity 和@Table

注释的实体 class
      public class Status{
      @Id
    @Column(name = "run_id")
    private int run_id; 

    @Column(name = "status")
    private String status; 

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_date" , updatable=false)
    private Date created;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_date" ,  insertable=false)
    private Date updated;

    @PrePersist
    protected void onCreate() {
        created = new Date();
    }


    @PreUpdate
    protected void onUpdate() {
        updated = new Date();
    }
//Getters and setters here
}

实施 class 是

  sessionFactory.getCurrentSession().save(status);  

方法二 使用@CreationTimeStamp 和@updatedTimeStamp。但是maven依赖

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.0-Final</version>
</dependency>

不将这些注释添加到 classpath

您使用 session.save() 方法而不是实体管理器是否有原因?我将 post 我的应用程序示例使用实体管理器来保存和合并实体。此外,我使用 java.time.LocalDateTime 而不是 java.util.Date,这就是为什么我不需要 @Temporal

这也可能有帮助:How to use @PrePersist and @PreUpdate on Embeddable with JPA and Hibernate 如果您想使用实体管理器,这将有所帮助:Guide to the Hibernate EntityManager 实体 class:

public abstract class AbstractEntity implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(updatable = false, nullable = false)
  private Long id;

  @Column
  private LocalDateTime createdTimestamp;

  @Column
  private LocalDateTime modifiedTimestamp;

  @Version
  private Long version;

  @PrePersist
  public void setCreationDateTime() {
    this.createdTimestamp = LocalDateTime.now();
  }

  @PreUpdate
  public void setChangeDateTime() {
    this.modifiedTimestamp = LocalDateTime.now();
  }
  //Getter and setter
}

抽象数据库服务class:

public abstract class AbstractDatabaseService {
  @PersistenceContext(name = "examplePU")
  protected EntityManager entityManager;
}

示例实体存储库接口:

public interface ExampleRepository {
  ExampleEntity save(ExampleEntity exampleEntity);
}

示例实体存储库实现:

public class ExampleRepositoryImpl extends AbstractDatabaseService implements ExampleRepository , Serializable {
  @Transactional
  @Override
  public ExampleEntity save(ExampleEntity exampleEntity) {
    ExampleEntity toPersist;
    // Updating an already existing entity
    if (exampleEntity.getId() != null) {
      toPersist = entityManager.find(ExampleEntity .class, exampleEntity.getId());
      // Omitted merging toPersist with the given exampleEntity through a mapper class here
    } else {
      toPersist = exampleEntity;
    }
    try {
      toPersist = entityManager.merge(toPersist);
    } catch (Exception e) {
      // Logging e
    }
    return toPersist;
  }
}

希望这对您有所帮助。