I got a UML diagram that looks like this:
Member<-----have one------>MemberAccount<-----subscribe to many----->Group
so i mapped it like that:
Class--------------------------------Component-------------------------------------Set
it's obviously a bidirectionnal many-to-many association between Group and MemberAccount:
Here's partially my mappings files :
1. Group.hbm.xml
<class
<set name="memberAccount" table="users_groups" lazy="true"
inverse="true" cascade="save-update">
<key column="group_id" />
<many-to-many class="src.users.Member" column="user_id"></many-to-many>
</set>
2. Member.hbm.xml
<component name="MemberAccount" class="src.accounts.MemberAccount">
<property name="status" type="string" />
<property name="creationDate" type="date" update="false"/>
<set name="myGroups" table="users_groups" lazy="true">
<key column="user_id" />
<many-to-many class="src.groups.Group" column="group_id">
</many-to-many>
</set>
</component>
But here's the problem : I create a member with its account, I create a group, I associate them
group.addMemberAccount(this);
myGroups.add(group);
and after the storage, i load the the too objects. I retrieved the group from the member passing through the component account BUT cannot do the opposite.
How can I do, any example for such mapping ???
here's the logs
[CascadingAction] cascading to saveOrUpdate: src.users.Member
Exception in thread "main" org.hibernate.HibernateException: instance not of expected entity type: src.users.Member
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3301)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1311)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:460)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at src.tests.HibernateTest.associateGroupMember(HibernateTest.java:118)
at src.tests.HibernateTest.scenario1(HibernateTest.java:39)
at src.tests.HibernateTest.main(HibernateTest.java:20)
|