英文:
JPA @PreUpdate @Persist seems not working as expected
问题
我遇到了一个使用 @PreUpdate 和 @PrePersist 来填充审计字段的问题。例如,当我想更新客户实体时,字段 updatedBy 和 updatedAt 仍然是 null;尽管在调试时,带有 @PreUpdate 注解的 preUpdate() 方法的代码被执行。
以下是负责在每个 JPA 实体中创建/更新审计字段的 AuditingField 类的代码:
@Embeddable
@Getter
@Setter
@NoArgsConstructor
public class FieldAuditing implements Serializable {
@Column(name = "DATE_CREATION", updatable = false)
private Instant createdAt;
@Column(name = "DATE_MODIFICATION", updatable = false)
private Instant updatedAt;
@Column(name = "DATE_SUPRESSION", updatable = false)
private Instant deletedAt;
@Column(name = "AUTEUR_CREATION", updatable = false, length = 100)
private String createdBy;
@Column(name = "AUTEUR_MODIFICATION", updatable = false, length = 100)
private String updatedBy;
@Column(name = "AUTEUR_SUPRESSION", updatable = false, length = 100)
private String deletedBy;
@Column(name = "IS_SUPPRIMER", nullable = false, updatable = false)
private boolean isDeleted;
@PrePersist
public void prePersist() {
setCreatedAt(Instant.now());
setCreatedBy(LoggedInUser.get());
}
@PreUpdate
public void preUpdate() {
setUpdatedAt(Instant.now());
setUpdatedBy(LoggedInUser.get());
}
@PreRemove
public void preRemove() {
setDeletedAt(Instant.now());
setDeleted(true);
setDeletedBy(LoggedInUser.get());
}
}
包含嵌入式审计字段的 client 实体:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "TF_CLIENT", schema = "dbo")
public class Client implements Serializable {
private static final long serialVersionUID = 8832848102370267801L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name = "CLT_ID", nullable = false)
private Long id;
@Column(name = "CLT_LIBELLE", nullable = false, length = 50, unique = true)
private String libelle;
@Temporal(TemporalType.DATE)
@Column(name = "CLT_DT_OUVERTURE", nullable = false)
private Date dateOuverture;
@Temporal(TemporalType.DATE)
@Column(name = "CLT_DT_FERMETURE")
private Date dateFermeture;
@Column(name = "CLT_B_ACTIF")
private boolean isActif;
@Embedded
private FieldAuditing fieldAuditing = new FieldAuditing();
//... 其他属性
}
更新 client 实体的方法:
private ClientDto save(ClientDto clientDto, Client client) {
startDateShouldBeBeforeEndDate(clientDto);
hasUniqueCodePaies(clientDto.getCodePaies());
Client clientSaved = clientRepository.save(clientMapper.toEntity(clientDto, client));
clientMapper.addOrRemoveClientActions(clientDto, clientSaved);
clientMapper.addOrRemoveClientEtats(clientDto, clientSaved);
clientRepository.save(clientSaved);
clientDto.setId(clientSaved.getId());
return clientDto;
}
最后是持久性上下文配置:
@Configuration
@PropertySource({"classpath:application.yml"})
@EnableJpaRepositories(
basePackages = "com.github.maaoutir.clientManager",
entityManagerFactoryRef = "mainEntityManager")
public class PersistenceContext {
private final Environment env;
public PersistenceContext(Environment env) {
this.env = env;
}
@Bean
@Primary
public DataSource mainDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("spring.datasource.driverClassName")));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean mainEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(mainDataSource());
em.setPackagesToScan("com.github.maaoutir.clientManager");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
// properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("spring.jpa.hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(mainEntityManager().getObject());
return transactionManager;
}
}
感谢您的任何帮助。
英文:
I have a problem for filling auditing fields by using @PreUpdate and @PrePersist. For instance When I'd like to update a client entity, the field updatedBy and updatedAt are still null; despite when I debug, the code of preUpdate() which is annotated with @PreUpdate is executed.
Below the code of AuditingField which is responsible for creating/updating the auditing fields in each JPA entity:
@Embeddable
@Getter
@Setter
@NoArgsConstructor
public class FieldAuditing implements Serializable {
@Column(name = "DATE_CREATION", updatable = false)
private Instant createdAt;
@Column(name = "DATE_MODIFICATION", updatable = false)
private Instant updatedAt;
@Column(name = "DATE_SUPRESSION", updatable = false)
private Instant deletedAt;
@Column(name = "AUTEUR_CREATION", updatable = false, length = 100)
private String createdBy;
@Column(name = "AUTEUR_MODIFICATION", updatable = false, length = 100)
private String updatedBy;
@Column(name = "AUTEUR_SUPRESSION", updatable = false, length = 100)
private String deletedBy;
@Column(name = "IS_SUPPRIMER", nullable = false, updatable = false)
private boolean isDeleted;
@PrePersist
public void prePersist() {
setCreatedAt(Instant.now());
setCreatedBy(LoggedInUser.get());
}
@PreUpdate
public void preUpdate() {
setUpdatedAt(Instant.now());
setUpdatedBy(LoggedInUser.get());
}
@PreRemove
public void preRemove() {
setDeletedAt(Instant.now());
setDeleted(true);
setDeletedBy(LoggedInUser.get());
}
}
The client entity that contains embedded auditing fields:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name="TF_CLIENT", schema="dbo")
public class Client implements Serializable {
private static final long serialVersionUID = 8832848102370267801L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator="native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name = "CLT_ID", nullable = false)
private Long id;
@Column(name = "CLT_LIBELLE", nullable = false, length = 50, unique = true)
private String libelle;
@Temporal(TemporalType.DATE)
@Column(name = "CLT_DT_OUVERTURE", nullable = false)
private Date dateOuverture;
@Temporal(TemporalType.DATE)
@Column(name = "CLT_DT_FERMETURE")
private Date dateFermeture;
@Column(name = "CLT_B_ACTIF")
private boolean isActif;
@Embedded
private FieldAuditing fieldAuditing = new FieldAuditing() ;
//... rest of another attributes
}
The method that updates the client entity
private ClientDto save(ClientDto clientDto, Client client) {
startDateShouldBeBeforeEndDate(clientDto);
hasUniqueCodePaies(clientDto.getCodePaies());
Client clientSaved = clientRepository.save(clientMapper.toEntity(clientDto, client));
clientMapper.addOrRemoveClientActions(clientDto, clientSaved);
clientMapper.addOrRemoveClientEtats(clientDto, clientSaved);
clientRepository.save(clientSaved);
clientDto.setId(clientSaved.getId());
return clientDto;
}
And finally the persistence context configuration:
@Configuration
@PropertySource({"classpath:application.yml"})
@EnableJpaRepositories(
basePackages = "com.github.maaoutir.clientManager",
entityManagerFactoryRef = "mainEntityManager")
public class PersistenceContext {
private final Environment env;
public PersistenceContext(Environment env) {
this.env = env;
}
@Bean
@Primary
public DataSource mainDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("spring.datasource.driverClassName")));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean mainEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(mainDataSource());
em.setPackagesToScan("com.github.maaoutir.clientManager");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
// properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("spring.jpa.hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(mainEntityManager().getObject());
return transactionManager;
}
}
I'm thankful for any help.
答案1
得分: 2
你正在对这些列使用 updatable=false
:
@Column(name = "DATE_MODIFICATION", updatable = false)
private Instant updatedAt;
@Column(name = "AUTEUR_MODIFICATION", updatable = false, length = 100)
private String updatedBy;
这意味着 JPA 不会使用该字段来更新列。根据 JPA 规范中关于 updatable
的说明:
> 是否在持久化提供程序生成的 SQL UPDATE 语句中包含该列。
这对于 createdBy
或 createdAt
列是有意义的,它们在 @PrePersist 中设置,并在第一次 INSERT 时持久化,之后您不希望对它们进行修改。但是,如果将列更新在 @PreUpdate(或 @PreRemove)中,并且将 updatable
设置为 false
,则不会使用 UPDATE 语句更新这些列。
英文:
You are using updatable=false
on those columns:
@Column(name = "DATE_MODIFICATION", updatable = false)
private Instant updatedAt;
@Column(name = "AUTEUR_MODIFICATION", updatable = false, length = 100)
private String updatedBy;
This means that JPA doesn't use this field to update the column. From the JPA spec for updatable
:
> Whether the column is included in SQL
UPDATE statements generated by the persistence
provider.
This make sense for the createdBy
or createdAt
columns, that are set on @PrePersist and persisted with the first INSERT, and you don't want them to be modified afterwards. But columns updated in @PreUpdate (or @PreRemove) wont be updated with the UPDATE statement if updatable
is set to false
答案2
得分: 0
请检查您的配置中是否有 @EnableJpaAuditing
,以及您的实体类是否有 @EntityListeners(AuditingEntityListener.class)
。
英文:
Check if you have @EnableJpaAuditing
on your configuration, and @EntityListeners(AuditingEntityListener.class)
on your entity class.
答案3
得分: 0
在类@Embeddable中创建一个带有super的构造函数。
对我有效。
public FieldAuditing(){
super();
}
英文:
Create a constructor in the class @Embeddable with super.
It worked for me.
public FieldAuditing(){
super();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论