vij*_*hmi 1 spring hibernate javax.persistence
我需要使用 createdDate 和 updatedDate 更新 postgres DB 我尝试使用方法 1,但它插入了空值。当我阅读时,似乎@prepersist 注释不适用于会话。
所以我决定使用方法 2 :Hibernate @CreationTimeStamp Annotation,我添加了 hibernate-annotations maven 依赖项,但是 @CreationTimeStamp 没有解析并给出编译错误。
有人可以建议我如何解决问题吗?
方法 1 使用 @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
}
Run Code Online (Sandbox Code Playgroud)
实现类是
sessionFactory.getCurrentSession().save(status);
Run Code Online (Sandbox Code Playgroud)
使用@CreationTimeStamp 和@updatedTimeStamp 的方法2。但是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>
Run Code Online (Sandbox Code Playgroud)
不会将这些注释添加到类路径
小智 6
您是否有理由使用 session.save() 方法而不是实体管理器?我将发布一个使用 entitymanager 来持久化和合并实体的应用程序示例。我也使用java.time.LocalDateTime代替java.util.Date,这就是为什么我不需要@Temporal.
这也可能有帮助:How to use @PrePersist and @PreUpdate on Embeddable with JPA and Hibernate 如果您想使用 entitymanager 这将有所帮助:Hibernate EntityManager Entity 类指南:
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
}
Run Code Online (Sandbox Code Playgroud)
抽象数据库服务类:
public abstract class AbstractDatabaseService {
@PersistenceContext(name = "examplePU")
protected EntityManager entityManager;
}
Run Code Online (Sandbox Code Playgroud)
示例实体存储库接口:
public interface ExampleRepository {
ExampleEntity save(ExampleEntity exampleEntity);
}
Run Code Online (Sandbox Code Playgroud)
示例实体存储库实现:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
3426 次 |
| 最近记录: |