@Createdby @CreatedDate 在实体更新后为空。

huangapple go评论103阅读模式
英文:

@Createdby @CreatedDate are null after entity is updated

问题

我已在Spring应用程序中实现了审计列,在创建新实体时它是有效的,列会被设置,但是在更新实体时,我发现两个@Createdby和@CreatedDate被设置为null,我该如何在创建后使这些列不可更改:

  1. @MappedSuperclass
  2. public abstract class Audit implements Serializable {
  3. @CreatedBy
  4. private String createdBy;
  5. @CreatedDate
  6. private Date createdDate;
  7. @LastModifiedBy
  8. private String lastModifiedBy;
  9. @LastModifiedDate
  10. private Date lastModifiedDate;
  11. }
英文:

I've implemented auditing columns in a Spring app, and it works when i create a new entity the columns are set, but when i update the entity i find the two @Createdby and @CreatedDate are set to null, how can i make these columns unchangeable after creation :

  1. @MappedSuperclass
  2. public abstract class Audit implements Serializable {
  3. @CreatedBy
  4. private String createdBy;
  5. @CreatedDate
  6. private Date createdDate;
  7. @LastModifiedBy
  8. private String lastModifiedBy;
  9. @LastModifiedDate
  10. private Date lastModifiedDate;
  11. }

答案1

得分: 5

对我而言,这是有效的:

  1. @MappedSuperclass
  2. public class Auditor {
  3. @Column(name = "created_date", updatable = false)
  4. @CreatedDate
  5. private long createdDate;
  6. @Column(name = "modified_date")
  7. @LastModifiedDate
  8. private long modifiedDate;
  9. @Column(name = "created_by", updatable = false)
  10. @CreatedBy
  11. private String createdBy;
  12. @Column(name = "modified_by")
  13. @LastModifiedBy
  14. private String modifiedBy;
  15. }

然后扩展该类:

  1. @Entity
  2. @EntityListeners(AuditingEntityListener.class)
  3. public class Material extends Auditor {
  4. @Id
  5. @SequenceGenerator(name = "seq_material", sequenceName = "seq_material")
  6. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_material")
  7. private Long id;
  8. @Column
  9. @NotBlank(message = "Nome é um campo obrigatório!", groups = validacaoProd.class)
  10. private String nome;
  11. // 更多内容...
  12. }

之后我在数据库中更新了记录:

  1. update material set created_date = 1618418750189, modified_date = 1618418750189
英文:

For me it worked:

  1. @MappedSuperclass
  2. public class Auditor {
  3. @Column(name = "created_date", updatable = false)
  4. @CreatedDate
  5. private long createdDate;
  6. @Column(name = "modified_date")
  7. @LastModifiedDate
  8. private long modifiedDate;
  9. @Column(name = "created_by", updatable = false)
  10. @CreatedBy
  11. private String createdBy;
  12. @Column(name = "modified_by")
  13. @LastModifiedBy
  14. private String modifiedBy;
  15. }

And extending the Class:

  1. @Entity
  2. @EntityListeners(AuditingEntityListener.class)
  3. public class Material extends Auditor {
  4. @Id
  5. @SequenceGenerator(name = "seq_material", sequenceName = "seq_material")
  6. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_material")
  7. private Long id;
  8. @Column
  9. @NotBlank(message = "Nome é um campo obrigatório!.", groups = validacaoProd.class)
  10. private String nome;
  11. More...
  12. }

And I updated the records in the database :

  1. update material set created_date = 1618418750189, modified_date = 1618418750189

答案2

得分: 0

你可以在类级别使用 @DynamicUpdate。它只会考虑更新列,忽略其他列放入更新查询中。

参考链接:https://www.baeldung.com/spring-data-jpa-dynamicupdate

英文:

you can use @DynamicUpdate in class-level. It will consider only update columns and ignore others to put in UPDATE query.

Ref : https://www.baeldung.com/spring-data-jpa-dynamicupdate

答案3

得分: 0

  1. @Configuration
  2. @EnableJpaRepositories({"uz.dam.userservice.repositories"})
  3. @EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
  4. @EnableTransactionManagement
  5. public class DatabaseConfiguration {
  6. }
  7. this is configuration for Jpa AuditingEntityListener class
英文:
  1. @Configuration
  2. @EnableJpaRepositories({"uz.dam.userservice.repositories"})
  3. @EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
  4. @EnableTransactionManagement
  5. public class DatabaseConfiguration {
  6. }

this is configuration for Jpa AuditingEntityListener class

huangapple
  • 本文由 发表于 2020年9月30日 23:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64140558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定