这是我的数据库设计:
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
我希望能够为人员历史记录类重用大部分人员代码,因为这些属性是重复的。按照现在的方式,我应该考虑并封装人员属性吗?这样,我可以使用合成,而不必在Person类和PersonHistory类之间复制代码?
答案 0 :(得分:0)
我认为你应该避免重复你的数据库列,我认为它应该是:
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
如果你正在使用JPA,你的类可以像这样映射:
@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...
}
因此,在对象模型中,您将能够从人员历史记录中访问人员数据。