gpt4 book ai didi

spring - AuditingEntityListener 不适用于在 spring jpa 中扩展另一个抽象实体的实体

转载 作者:行者123 更新时间:2023-12-05 05:13:39 24 4
gpt4 key购买 nike

我在各自的字段上使用了@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,或者我在这里遗漏了什么......??

最佳答案

我在你的类中添加了@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 {
}

一切正常!

关于spring - AuditingEntityListener 不适用于在 spring jpa 中扩展另一个抽象实体的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53375607/

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