使用JPA数据CrudRepositories自动生成创建日期和上次修改日期 [英] Auto generating create date and last modify date using JPA data CrudRepositories

查看:561
本文介绍了使用JPA数据CrudRepositories自动生成创建日期和上次修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图迁移我的应用程序以使用实体生成模式,而不是具有预先存在的模式。到目前为止,create和lastModify日期由DB(MySql)更新,但现在我想从JPA生成模式,我必须以编程方式执行此操作。



之前,我总是使用@PrePersit和@PreUpdate来做这个没有任何问题,但现在它不工作,我认为这是因为我使用弹簧数据的CrudRepository接口为我的DAO,所以我不知道发生了什么实体经理在这里......



我做了一些调查,并且发现了一些关于春季审计的事情,但是我无法使其发挥作用。此刻我正在尝试@Created和@LastModified注释,但它们不起作用。这是我现在拥有的:我的可抽象性:

  @MappedSuperclass 
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity实现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)
私人创建日期;

@LastModifiedDate
@Version
// @Generated(GenerationTime.ALWAYS)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = LAST_MODIFIED,insertable = false,updatable = true)
private last lastModified;
$ b $ / **
*将自动生成的字段id,创建和上次修改后的形式给定实体/ DTO复制到此实体/ DTO
*
* @param copyEntity要复制的实体。
* /
public void copy(AbstractEntity copyEntity){
this.setId(copyEntity.getId());
this.setCreated(copyEntity.getCreated());
this.setLastModified(copyEntity.getLastModified());
}

}

我的配置:



$ $ p $ $ $ c $ @ $配置$ b $ @ActiveProfiles(开发)
@EnableTransactionManagement
@EnableJpaRepositories
@ EnableJpaAuditing(setDates = false,auditorAwareRef =auditorAware)
public class RepositoryTestContext {

@Bean
public AuditorAware< String> auditorAware(){
return new AuditorAware< String>(){

@Override
public String getCurrentAuditor(){
returndummy;
}
};


$ / code $ / pre

基本上我的测试显示创建日期和上次修改日期没有被更新。任何想法???我认为你在你的AbstractEntity上缺少@EntityListeners注解:

$ b $

解决方案

  @EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity实现Serializable {


I'm trying to migrate my app to generate the schema using the entities, instead of having a pre existing schema. Up until now, the create and lastModify dates where updated by the DB (MySql), but now since i want to generate the schema from JPA, i must do this programatically.

Before, i always used @PrePersit and @PreUpdate to do this without any problems but now it doesn't work and i think it's because i use spring data's CrudRepository Interfaces for my DAOs, so i don't know what's going on with the entity manager here...

I did some research and i found a few thing about spring auditing, but i couldn't get it to work. at the moment i'm trying the @Created and @LastModified annotations, but they don't work. this is what i have now: my abstractentity:

@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());
}

}

my configuration:

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

Basically my tests show that the create date and last modify date are not being updated. any ideas???

解决方案

I think you are missing the @EntityListeners annotation on your AbstractEntity :

@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {

这篇关于使用JPA数据CrudRepositories自动生成创建日期和上次修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆