I have a class 'Audit' that will hold the following details about an entity:
- Time Created
- Created by which application user
- Time Updated
- Updated by which application user
On the database, these fields are stored on the same table as the entity attributes. For example,
USER table:
CREATE TABLE USERS (
id BIGINT NOT NULL AUTO_INCREMENT,
display_name VARCHAR(30) NOT NULL,
active BOOLEAN NOT NULL DEFAULT FALSE,
created_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_user BIGINT NOT NULL DEFAULT 0,
updated_time TIMESTAMP NOT NULL DEFAULT 0,
updated_user BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
USER Class:
@Entity(name="USER")
@Table(name="USERS")
public class User implements Audited {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID", nullable=false, updatable=false)
private long id;
@Column(name="DISPLAY_NAME", nullable=false)
@NotNull
@Size(min=5, max=30)
private String displayName;
@Column(name="ACTIVE", nullable=false)
@NotNull
private boolean active;
private Audit audit;
}
AUDIT Class:
public class Audit {
private Date createdTime;
private User createdByUser;
private Date updatedTime;
private User updatedByUser;
}
On each entity table, the audit fields will be named the same. What is the best approach in mapping these fields?