In JPA, a map of nested values ​​that have an embedded object used as a key

I'm still new to JPA (and Hibernate, which I use 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;
}

The database has a collection table "Root_resources" in which the map values ​​are stored, but the resource type is displayed twice (in fact, the resource type identifier), once as a KEY to the map and once as part of the value.

Is there a way, like an annotation, for example, @MapKeyto indicate that a key is one of the columns of a value (i.e., embedded)?

+3
source share
1 answer

, - :

@MapKeyJoinColumn("RESOURCE_TYPE_ID")

ElementCollection.

FK .

0

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


All Articles