Hibernate version:
hibernate: 3.2.3.ga
hibernate-annotations: 3.2.1.ga
Mapping documents:
None
I have an entity [User] which has a property [Subspecialty].
Subspecialty is a simple java.lang.Enum
This I can persist without issue.
The problem is that I need a Collection of Subspecialty.
Code:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Collection<Subspecialty> getSubspecialties() {
return subspecialties;
}
public void setSubspecialties(Collection<Subspecialty> subspecialties) {
this.subspecialties = subspecialties;
}
This gives an exception:
...
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: ...model.User.subspecialties[...model.Subspecialty]
...
That is a little perplexing (though not entirely unexpected) given Hibernate is clever enough to persist the Subspecialty without any help in the first case.
Okay so Subspecialty needs to be an Entity? How do you annotate an enum? All combinations I've tried present one error or another. I can change it to a class with some static fields and it all works, but the point is to use an enum.
Before annotations, I used a UserType class to accomplish this persistence, and simply mapped the Subspecialty to the SubspecialtyUserType. But despite my best efforts, I have not been able to get the annotations to work (I know the SubspecialtyUserType will work if mapped properly).
So my question is: how do you persist a collection of enum using annotations?