Reputation: 1132
I have a field in a model object which takes an enum like this:
@Enumerated(...)
private UniveritySubject subject; //Of course not working like this...
For a better structure and a better overview I want to separate my subject in two enums: One enum representing winter and one enum representing summer subjects like this:
public class Subject {
public enum Summer {
...
MATH;
}
public enum Winter {
...
PHYSICS;
}
}
How can I achieve the mapping of either of those enums to the model column subject
? Sadly it is not possible to inherite enums, so I can not have a "superclass enum". I thought about using one enum with boolean value isInWinter
, but please regard that I have a lot of subjects and I think it isn't good to overview.
Is there any other solution?
Upvotes: 0
Views: 1357
Reputation: 12235
To normalize this a bit I would change the design. UniversitySubject
should be an entity of its own, like (for example):
@Entity
public class UniversitySubject {
@Enumerated(EnumType.STRING)
private Season season;
@Enumerated(EnumType.STRING)
private Subject subject;
}
where Season
is:
enum Season { SPRING, SUMMER, AUTUMN, WINTER }
and Subject
enum Subject { MATH, PHYSICS, BIOLOGY }
and then in the class using it would be like
@ManyToOne
private UniversitySubject subject;
Also, to clarify things a bit maybe UniversitySubject
could be named TermSubject
or something alike?
Of course it is possible to use JPA annotations for example to concat all this stuff into one string and parse back but small changes in the design should make these 'hacks' unnecessary.
Upvotes: 1