Annotate EnumMap <K, V> using Hibernate annotations

Just a quick question:

I want to use EnumMap in one of my entity classes.

Is there a special way to annotate them? What will happen to this if I do not comment on it explicitly?

More specifically: I want the key to be stored as String values, not int values.

amuses

+3
source share
2 answers

You can use the @MapKeyEnumerated (STRING) annotation for this purpose if the key for your card is Enum: http://download.oracle.com/javaee/6/api/javax/persistence/MapKeyEnumerated.html

+5
source

I will give an example for HashMap<Enum, List<Object>>

, Person

@Entity
@Table(name="Tasks")
@Access(AccessType.FIELD)
public class Task implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id; 

@OneToMany(cascade={CascadeType.ALL,CascadeType.PERSIST})
@MapKeyEnumerated(EnumType.STRING)
private Map<Role,PersonBag> persons;

[...]
}

PersonBag:

@Entity
@Table(name="Person_Bags")
public class PersonBag implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @ManyToMany
    @JoinColumns({
        @JoinColumn(name="PersonBag_Id",referencedColumnName="Id"),
        @JoinColumn(name="Person_Id",referencedColumnName="Id")
    })
    private List<Person> persons;
[...]
}
+2

Source: https://habr.com/ru/post/1781636/


All Articles