hellzone
hellzone

Reputation: 5236

How to save multiple enum value to database with JPA?

I have a Server class and I want to save server object's to database. I have created an entity like below but problem is there are unnecessary fields.

For example Linux Operating system records also has WindowsOperatingSystemVersion column. How can I save this multiple enum values to database efficiently?

@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
}

Upvotes: 0

Views: 850

Answers (1)

jklee
jklee

Reputation: 2268

Only enums is not a good design. This would be an alternative design, but I do not know the exact requirements.

@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
}

Upvotes: 2

Related Questions