I am new to Hibernate and JPA. I have several entities, each of which contains following four columns:
1. created_by
2. last_modified_by
3. created_date
4. last_modified_date
I would like these columns to get auto-populated while saving the associated entity.
Two sample entities are as follows:
Entity 1:
@Entity
@Table(name = "my_entity1")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MyEntity1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "created_by")
private String createdBy;
@Column(name = "last_modified_by")
private String lastModifiedBy;
@Column(name = "created_date")
private Instant createdDate;
@Column(name = "last_modified_date")
private String lastModifiedDate;
}
Entity 2:
@Entity
@Table(name = "my_entity2")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MyEntity2 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "description")
private String description;
@Column(name = "created_by")
private String createdBy;
@Column(name = "last_modified_by")
private String lastModifiedBy;
@Column(name = "created_date")
private Instant createdDate;
@Column(name = "last_modified_date")
private String lastModifiedDate;
}
In this context, I have gone through following posts: How to autogenerate created or modified timestamp field?, How can you make a created_at column generate the creation date-time automatically like an ID automatically gets created?.
I am getting how to capture the dates fields but I cannot understand how to capture created_by and last_modified_by.