In JPA, a Map of embeddable values, that have an embedded entity used as the key
Posted
by Schmuli
on Stack Overflow
See other posts from Stack Overflow
or by Schmuli
Published on 2010-05-02T18:29:09Z
Indexed on
2010/05/02
18:38 UTC
Read the original article
Hit count: 143
I'm still new to JPA (and Hibernate, which I'm using as my provider), so maybe this just can't be done, but anyway...
Consider the following code:
@Entity
class Root {
@Id
private long id;
private String name;
@ElementCollection
private Map<ResourceType, Resource> resources;
...
}
@Entity
class ResourceType {
@Id
private long id;
private String name;
}
@Embeddable
class Resource {
private ResourceType resourceType;
private long value;
}
In the database, there is a collection table, 'Root_resources', that stores the values of the map, but the resource type appears twice (actually, the resource type ID does), once as the KEY of the map, and once as part of the value.
Is there a way, similar to, say, the @MapKey
annotation, to indicate that the key is one of the columns of the value (i.e. embedded)?
© Stack Overflow or respective owner