简体   繁体   中英

Class design for domain model with audit trail

This is my DB design:

Person
------------------------------------------------
ID                UINT NOT NULL,
Name              VARCHAR(200) NOT NULL,
DOB               DATE NOT NULL,
Email             VARCHAR(100) NOT NULL


Person_History
------------------------------------------------
ID                UINT NOT NULL,
PersonID          UINT NOT NULL,
Name              VARCHAR(200) NOT NULL,
DOB               DATE NOT NULL,
Email             VARCHAR(100) NOT NULL
AuditID           UINT NOT NULL


Audit
------------------------------------------------
ID                UINT NOT NULL,
UserID            UINT NOT NULL,               -- Who
AffectedOn        DATE NOT NULL,               -- When
Comment           VARCHAR(500) NOT NULL        -- Why

I would like to be able to reuse most of the person code for the person history class, since the properties are duplicated. With the way it is now, should I be factoring out and encapsulating the person properties? This way, I can use composition, instead of having to duplicate code between a Person class and a PersonHistory class?

I believe you should avoid duplicating your DB columns in my opinion it should be:

Person
------------------------------------------------
ID                UINT NOT NULL,
Name              VARCHAR(200) NOT NULL,
DOB               DATE NOT NULL,
Email             VARCHAR(100) NOT NULL


Person_History
------------------------------------------------
ID                UINT NOT NULL,
PersonID          UINT NOT NULL,
AuditID           UINT NOT NULL


Audit
------------------------------------------------
ID                UINT NOT NULL,
UserID            UINT NOT NULL,               -- Who
AffectedOn        DATE NOT NULL,               -- When
Comment           VARCHAR(500) NOT NULL        -- Why

In case you're using JPA your classes could be mapped like this:

@Entity
@Table(name = "PERSON")
public class Person {

@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;

@Column(name = "DOB")
@Temporal(TemporalType.DATE)
private Date birthDate;

@Column(name = "EMAIL")
private String email;

@OneToMany(mappedBy="person", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private List<PersonHistory> personHistories;

// getters and setters...

}


@Entity
@Table(name = "PERSON_HISTORY")
public class PersonHistory {

@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_PERSON", nullable=false)
private Person person;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_AUDIT", nullable=false)
private Audit audit;

// getters and setters...

}


@Entity
@Table(name = "AUDIT")
public class Audit {

@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;

@Column(name = "AFFECTED_ON")
@Temporal(TemporalType.DATE)
private Date affectedOn;

@Column(name = "COMMENT")
private String comment;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_USER", nullable=false)
private User user;

// getters and setters...

}

So in your object model you will be able to access Person data from Person History.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM