Hi all,
I am trying to figure out the mapping configuration for a map containing a set. The schema I would like is:
create table entity (id bigint not null, primary key (id));
create table key (id bigint not null, primary key (id));
create table entity_key_values (entity_id bigint not null, key_id bigint not null, value integer not null);
And the field in the entity that I want to map would be:
Code:
class Entity {
...
Map<Key, Set<Integer>> keyValues = new ...;
...
};
Note that in my case, I do not wish there to be a unique constraint on the entity_key_values table for the entity_id & key_id pair, as there should be multiple rows that will all have different values to be mapped into the Set.
I am using annotations, and I can map a simple Map<Key, Integer> to this schema, using:
Code:
@CollectionOfElements
@JoinTable(
name = "entity_key_values",
joinColumns = @JoinColumn(name="entity_id")
)
@Column(name="value", nullable=false)
@MapKeyManyToMany(targetEntity=Key.class,
joinColumns = @JoinColumn(name="key_id")
)
Map<Key, Integer> keyValues = new ...;
But this (of course) this creates the entity_key_values join table with a primary key (unique) constraint on entity_id & key_id. And it doesn't work at all if I put the Set<> around Integer.
Any thoughts? If this isn't possible using annotations, is it possible using the xml configuration?
Also, if this mapping IS possible, can the value be a non-primitive type (eg. a Component or another Entity rather than a integer)?