AuditingEntityListener对于在Spring JPA中扩展了另一个抽象实体的实体不起作用

时间:2018-11-19 13:24:17

标签: spring spring-mvc spring-boot spring-data-jpa

我在各自的字段上使用了@ CreatedBy,@ CreatedDate,@ LastModifiedBy和@LastModifiedDate注释。通过使用@ MappedSuperclass,@ EntityListeners,我可以在列上方保持不变。

但这不适用于以下情况:

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
    @CreatedBy
    protected U createdBy;
    @CreatedDate
    @Temporal(TIMESTAMP)
    protected Date creationDate;
    @LastModifiedBy
    protected U lastModifiedBy;
    @LastModifiedDate
    @Temporal(TIMESTAMP)
    protected Date lastModifiedDate;
}

@Entity
@Table(name = "tabel1")
@PrimaryKeyJoinColumn(name = "ID")
class A extends B {

    @Column(name = "NAME1", nullable = false)
    private String name1;
    @Column(name = "CONTENT1", nullable = false)
    private String content1;
}

@Entity
@Table(name = "tabel2")
public abstract class B extends Auditable{
    @Id
    @GeneratedValue
    @Column(name = "ID", nullable = false)
    private int id;
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "CONTENT", nullable = false)
    private String content;
}

AuditorAwareImpl.java

public class AuditorAwareImpl implements AuditorAware<String>
{
    @Override
    public Optional<String> getCurrentAuditor()
    {
        return Optional.ofNullable("Saravanan");

    }

}

JpaAuditConfiguration.java

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditConfiguration
{
    @Bean
    public AuditorAware<String> auditorProvider()
    {

        return new AuditorAwareImpl();
    }
}

在这种情况下,实体B填充有审计列。但是实体A不是。有没有一种方法可以填充实体A或我在这里错过了任何东西?

1 个答案:

答案 0 :(得分:0)

我在您的课程中添加了 @Entity 批注:

@Entity
public class A extends B {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;
    private String content;

}

@Entity
public class B extends Auditable<String> {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;
    private String content;
}

持久性配置类(用于Spring Boot):

@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}

一切正常!