Spring 使用JPA数据积件库自动生成创建日期和上次修改日期
我正在尝试迁移我的应用程序,以使用实体生成模式,而不是使用预先存在的模式。到目前为止,create和lastModify都是由DB(MySql)更新的日期,但是现在因为我想从JPA生成模式,所以我必须通过编程来完成这项工作 以前,我总是使用@PrePersit和@PreUpdate来做这件事,没有任何问题,但现在它不起作用了,我想这是因为我在DAO中使用了spring数据的Crudepository接口,所以我不知道这里的实体管理器是怎么回事 我做了一些研究,发现了一些关于spring审计的东西,但我无法让它发挥作用。目前我正在尝试@Created和@LastModified注释,但它们不起作用。这就是我现在拥有的:我的抽象实体:Spring 使用JPA数据积件库自动生成创建日期和上次修改日期,spring,hibernate,jpa,auditing,Spring,Hibernate,Jpa,Auditing,我正在尝试迁移我的应用程序,以使用实体生成模式,而不是使用预先存在的模式。到目前为止,create和lastModify都是由DB(MySql)更新的日期,但是现在因为我想从JPA生成模式,所以我必须通过编程来完成这项工作 以前,我总是使用@PrePersit和@PreUpdate来做这件事,没有任何问题,但现在它不起作用了,我想这是因为我在DAO中使用了spring数据的Crudepository接口,所以我不知道这里的实体管理器是怎么回事 我做了一些研究,发现了一些关于spring审计的东西
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(name = "seq")
@Column(name = "ID")
private Long id = 0L;
@CreatedDate
// @Generated(GenerationTime.INSERT)
@Column(name = "CREATED", insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@LastModifiedDate
@Version
// @Generated(GenerationTime.ALWAYS)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LAST_MODIFIED", insertable = false, updatable = true)
private Date lastModified;
/**
* copies the auto generated fields id, created and last modified form the given entity/DTO to this entity/DTO
*
* @param copyEntity the entity to copy from.
*/
public void copy(AbstractEntity copyEntity) {
this.setId(copyEntity.getId());
this.setCreated(copyEntity.getCreated());
this.setLastModified(copyEntity.getLastModified());
}
}
我的配置:
@Configuration
@ActiveProfiles("development")
@EnableTransactionManagement
@EnableJpaRepositories
@EnableJpaAuditing(setDates = false, auditorAwareRef = "auditorAware")
public class RepositoryTestContext {
@Bean
public AuditorAware<String> auditorAware() {
return new AuditorAware<String>() {
@Override
public String getCurrentAuditor() {
return "dummy";
}
};
}
}
@配置
@ActiveProfiles(“开发”)
@启用事务管理
@授权代理
@启用JPA审核(setDates=false,auditorAwareRef=“auditorAware”)
公共类RepositoryTestContext{
@豆子
公共AuditorAware AuditorAware(){
返回新的AuditorAware(){
@凌驾
公共字符串getCurrentAuditor(){
返回“dummy”;
}
};
}
}
基本上,我的测试显示创建日期和上次修改日期没有被更新。有什么想法吗?我认为您缺少AbstractEntity上的@EntityListeners注释:
@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {
我认为您缺少AbstractEntity上的@EntityListeners注释:
@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {
我认为这些注释是Hibernate5.2+++ 我想你可能正在使用Hibernate 5.0.x或更低的版本
不过我不能肯定没有看到你的POM。只需确保当前版本支持hibernate.annotations中的此注释。我认为这些注释是hibernate 5.2+++ 我想你可能正在使用Hibernate 5.0.x或更低的版本
不过我不能肯定没有看到你的POM。只需确保您当前的版本支持hibernate.annotations中的此批注。我想您已经在这行中设置了它: @启用JPA审核(setDates=false,auditorAwareRef=“auditorAware”) 这是Spring文档中的常见问题: 问:我想使用SpringDataJPA审计功能,但我的数据库已经设置好,可以设置实体的修改和创建日期。如何防止Spring数据以编程方式设置日期
回答:只需将审核名称空间元素的set dates属性设置为false。我想您已经在这行中设置了它: @启用JPA审核(setDates=false,auditorAwareRef=“auditorAware”) 这是Spring文档中的常见问题: 问:我想使用SpringDataJPA审计功能,但我的数据库已经设置好,可以设置实体的修改和创建日期。如何防止Spring数据以编程方式设置日期
回答:只需将审核名称空间元素的set dates属性设置为false。对于@EntityListeners中类周围的括号有什么特殊用途吗?括号在那里是因为@EntityListeners接受类数组。对于@EntityListeners中类周围的括号有什么特殊用途吗?括号在那里因为@EntityListeners接受一个类数组。