Reputation: 605
I am trying to create a sample Report card application and want to persist a map between subject and student's grade This is my scorecard class:
@Entity
public class ScoreCard {
@NotNull @ElementCollection @ManyToMany(cascade=CascadeType.ALL)
private Map<Subject, String> gradesForMainSubject = new HashMap<Subject, String>();
}
But when trying to save data I always end up with
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: gradesForMainSubject
Subject itself is a Managed entity (annotated by @Entity
). Any suggestions on how can I move forward.
Upvotes: 2
Views: 3372
Reputation: 4154
You cannot use both @ElementCollection and @ManyToMany at the same time for a collection field.
If the values of your collection are entities, then you can use either one of the 2: @OneToMany or @ManyToMany
If the values of your collection are non-entities, then you must use @ElementCollection.
In your case, the values of your map are String which are not entities. Therefore you need to use @ElementCollection. Remove the @ManyToMany mapping. This rule should be followed, regardless of whether you map key is an entity or not.
Upvotes: 4