Hello everybody
I have a question concerning a mapping of Map<@Entity, enum>.
My mapping is specified as:
Code:
public class MyEntityClass {
private Map<OtherEntityClass, RelationEnum> relatedEntities = new HashMap<OtherEntityClass, RelationEnum>();
...
@ManyToMany
@JoinTable(name="TBL_ASSOCIATED_ENTITIES",
joinColumns=@JoinColumn(name="MY_ENTITY_ID"),
inverseJoinColumns={@JoinColumn(name="OTHER_ENTITY_ID")})
@MapKeyManyToMany(joinColumns={@JoinColumn(name="OTHER_ENTITY_ID")})
@Column(name="RELATION_ENUM_ID")
public Map<OtherEntityClass, RelationEnum> getRelatedEntities () {
return this.relatedEntities;
}
}
If I use @ManyToMany (the appropriate association with the OtherEntityClass), I get an exception:
Quote:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: ...OtherEntityClass[RelationEnum]
....
Looks like Hibernate expects an @Entity as the map value too, not only for the key. I couldn't find a way to declare the enum such that it will persist to a column under @ManyToMany, at least not via annotations.
The enum is describing the type of relation between the two entities so there is no reason why to create a new entity just for that.
One way to workaround this is using @CollectionOfElements (omitting the inverseJoinColumn attr in @JoinTable). While this works, I don't think it's the proper solution because @CollectionOfElements relates mainly to embedded fields, not separate Entities and there are several implications - for example, I can't use the mappedBy attr.
Is there a way to directly persist a Map<@Entity, enum> using @ManyToMany?
Thanks,
Yuval