gpt4 book ai didi

spring - 在spring + hibernate中保存creationTimestamp和updatedTime

转载 作者:行者123 更新时间:2023-12-01 21:55:28 25 4
gpt4 key购买 nike

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

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

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

方法一使用@Entity 和@Table 注解的实体类

      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
}

实现类是

  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>

不将这些注释添加到类路径

最佳答案

您使用 session.save() 方法而不是 entitymanager 有什么原因吗?我将发布一个使用实体管理器来持久化和合并实体的应用程序示例。我也在使用 java.time.LocalDateTime而不是 java.util.Date ,这就是为什么我不需要 @Temporal .

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

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
}

抽象数据库服务类:

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

示例实体存储库接口(interface):

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;
}
}

希望这对您有所帮助。

关于spring - 在spring + hibernate中保存creationTimestamp和updatedTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57769936/

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