Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Createdby @CreatedDate are null after entity is updated

Tags:

java

spring

jpa

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;
}
like image 611
Bilal Dekar Avatar asked Jan 19 '26 01:01

Bilal Dekar


1 Answers

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 
like image 135
Rogério Viana Avatar answered Jan 20 '26 17:01

Rogério Viana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!