Imagine I have the following class where all the fields are integers:
@Entity
public class Bar implements Serializable {
@Id
private Integer id;
@Basic
private Integer field1;
@Basic
private Integer field2;
//Constructor & getters/setters omitted
}
Now I wish to have another class Foo
which has a HashMap where the keys are fields in Bar
with the corresponding values. I.e., something like this:
@Entity
public class Foo implements Serializable {
@Id
private Integer id;
@Basic
private String someString;
@Basic
private Integer someInteger;
@??
private HashMap<String, Integer> barMap;
//Constructor & getters/setters omitted
The reason behind doing this is that I have an an underlying Enum, each value of the Enum should be a column, and then after fetching this as a Map I would have easy access to every field because I already know the names of the properties.
In my mind, the underlying tables would look something like this: Link to image, as I am not allowed to embed yet.
Is something like this possible? What kind of annotation would it require? I previously tried something as follows in Foo
, but it reports that a One-To-One attribute should not be used as a map, so I am probably thinking about this the wrong way.
@OneToOne(mappedBy="Bar")
@MapKey(name="Id")
private HashMap<String, Integer> barMap;