tags:

views:

53

answers:

1

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)?

A: 

I think what you're asking for is something like:

@MapKeyJoinColumn("RESOURCE_TYPE_ID")

on the ElementCollection.

Then also specify the column name for the FK mapping of the resource type on your embeddable.

Affe