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

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

@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

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:

确定