I wonder if it is possible to create a many-to-many relationship between an object type and an enum type. There seems to be a more general obstruction with retrieving the list of enum values bound to object, because we cannot have a public constructor in enum type
---
My Hibernate version is 3.6.3.
When I access the roles enum set in user object type I get the following error:
Exception in thread "main" org.hibernate.InstantiationException: No default constructor for entity: foo.MyRole at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:107) at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:123) at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:603) at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3911) at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1422) at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1411) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1446) at org.hibernate.loader.Loader.getRow(Loader.java:1355) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:611) at org.hibernate.loader.Loader.doQuery(Loader.java:829) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) at org.hibernate.loader.Loader.loadCollection(Loader.java:2166) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
The object is MyUser and it has a list of MyRole's of enum type.
@Entity public enum MyRole { @Enumerated(EnumType.ORDINAL) USER , @Enumerated(EnumType.ORDINAL) ADMIN;
@Id public int getRoleId() { return ordinal(); }
public void setRoleId(int roleId) { }
}
@Entity public class MyUser {
private long userId;
@OneToMany(cascade=CascadeType.ALL) private Set<MyRole> roles = new HashSet<MyRole>();
...
}
I define the many-to-many relationship in the User mapping file: <set name="roles" table="MYUSER_MYROLE" cascade="all"> <key column="USERID" /> <many-to-many column="ROLEID" class="foo.MyRole" /> </set>
|