英文:
@Createdby @CreatedDate are null after entity is updated
问题
我已在Spring应用程序中实现了审计列,在创建新实体时它是有效的,列会被设置,但是在更新实体时,我发现两个@Createdby和@CreatedDate被设置为null,我该如何在创建后使这些列不可更改:
@MappedSuperclass
public abstract class Audit implements Serializable {
@CreatedBy
private String createdBy;
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private Date lastModifiedDate;
}
英文:
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 :
@MappedSuperclass
public abstract class Audit implements Serializable {
@CreatedBy
private String createdBy;
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private Date lastModifiedDate;
}
答案1
得分: 5
对我而言,这是有效的:
@MappedSuperclass
public class Auditor {
@Column(name = "created_date", updatable = false)
@CreatedDate
private long createdDate;
@Column(name = "modified_date")
@LastModifiedDate
private long modifiedDate;
@Column(name = "created_by", updatable = false)
@CreatedBy
private String createdBy;
@Column(name = "modified_by")
@LastModifiedBy
private String modifiedBy;
}
然后扩展该类:
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Material extends Auditor {
@Id
@SequenceGenerator(name = "seq_material", sequenceName = "seq_material")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_material")
private Long id;
@Column
@NotBlank(message = "Nome é um campo obrigatório!", groups = validacaoProd.class)
private String nome;
// 更多内容...
}
之后我在数据库中更新了记录:
update material set created_date = 1618418750189, modified_date = 1618418750189
英文:
For me it worked:
@MappedSuperclass
public class Auditor {
@Column(name = "created_date", updatable = false)
@CreatedDate
private long createdDate;
@Column(name = "modified_date")
@LastModifiedDate
private long modifiedDate;
@Column(name = "created_by", updatable = false)
@CreatedBy
private String createdBy;
@Column(name = "modified_by")
@LastModifiedBy
private String modifiedBy;
}
And extending the Class:
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Material extends Auditor {
@Id
@SequenceGenerator(name = "seq_material", sequenceName = "seq_material")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_material")
private Long id;
@Column
@NotBlank(message = "Nome é um campo obrigatório!.", groups = validacaoProd.class)
private String nome;
More...
}
And I updated the records in the database :
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
@Configuration
@EnableJpaRepositories({"uz.dam.userservice.repositories"})
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {
}
this is configuration for Jpa AuditingEntityListener class
英文:
@Configuration
@EnableJpaRepositories({"uz.dam.userservice.repositories"})
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {
}
this is configuration for Jpa AuditingEntityListener class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论