我有一个Server类,我想将服务器对象保存到数据库。我创建了一个像下面这样的实体,但问题是有不必要的字段。
例如Linux操作系统记录也有WindowsOperatingSystemVersion列。如何有效地将多个枚举值保存到数据库?
@Entity
@Table(name = "server")
public class Server{
@Column
private OperatingSystem os;
@Column
private LinuxOperatingSystemVersion losv;
@Column
private WindowsOperatingSystemVersion wosv;
}
public enum OperatingSystem{
LINUX,
WINDOWS
}
public enum LinuxOperatingSystemVersion{
UBUNTU,
SOLARIS
}
public enum WindowsOperatingSystemVersion{
WINDOWS10,
XP
}
答案 0 :(得分:2)
只有枚举不是一个好设计。这将是一种替代设计,但我不知道确切的要求。
@Entity
public class Server {
@OneToMany
OperatingSystem operatingSystem;
}
@Entity
public class OperatingSystem {
@Enumerated(EnumType.STRING)
OperatingSystemEnum operatingSystemEnum;
@Enumerated(EnumType.STRING)
OperatingSystemVersion operatingSystemVersion;
}
public enum OperatingSystemEnum {
LINUX,
WINDOWS
}
public enum OperatingSystemVersion {
UBUNTU,
SOLARIS,
WINDOWS10,
XP
}