Just a quick ques开发者_StackOverflow社区tion:
I want to use an EnumMap in one of my entity classes.
Is there a special way to annotate these? What happens to it if I don't annotate it explicitly?
More specific: I want the Key to be persisted as String values rather than int values.
cheers
You can use the annotation @MapKeyEnumerated(STRING) for this purpose, if the key for your map is an Enum: http://download.oracle.com/javaee/6/api/javax/persistence/MapKeyEnumerated.html
I submit an example for a HashMap<Enum, List<Object>>
In this case, the object is a custom class, 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;
[...]
}
精彩评论