• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What am I doing wrong?

 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have what I think is a very simple many-to-many relationship, 2 tables "m_events" & "m_speakers" joined in table "event_speakers"

////////////////////////////////////////////////////////////////
<hibernate-mapping>
<class name="test.SpeakerManyToMany" table="m_speakers">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="firstName" type="string" length="20"/>
<property name="lastName" type="string" length="20"/>
<set name="events" table="event_speakers" cascade="all">
<key column="speaker_id"/>
<many-to-many class="test.EventManyToMany"/>
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="test.EventManyToMany" table="m_events">
<id name="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" type="string" length="100"/>

<set name="speakers" table="event_speakers" cascade="all" inverse="true">
<key column="event_id"/>
<many-to-many class="test.SpeakerManyToMany"/>
</set>
</class>
</hibernate-mapping>

////////////////////////////////////////////////////////////////

Test code -

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

EventManyToMany event = new EventManyToMany();
event.setName("Inverse test");

event.getSpeakers().add(new SpeakerManyToMany("John", "Smith", event));
event.getSpeakers().add(new SpeakerManyToMany("Dave", "Smith", event));
event.getSpeakers().add(new SpeakerManyToMany("Joan", "Smith", event));

session.save(event);

tx.commit();
HibernateUtil.closeSession();

HibernateUtil.sessionFactory.close();

////////////////////////////////////////////////////////////////

All 3 tables are populated but the event_id column in "event_speakers" is null and speaker_id is set correctly.

If I move inverse="true" in to the first file then event_id column in "event_speakers" is now set correct but speaker_id is null

inverse="true" in both files does not insert a record into "event_speakers"!

How can I do this so both event_id & speaker_id are set correctly? - this is really doing my head in, I've tried every combinantion I can think of but it just doesn't for what I want!

Probably something stupid I'm doing/not doing but any ideas?

thanks

harry
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's it then - tried everything I can find & there is no way on this earth hibernate will populate event_id & speaker_id fields correctly!

Is this just a specifc problem with Oracle or is it a bug with many-to-many relationships in hibernate in general?

Has anyone else come across this with Oracle? has anybody any advice? should I drop hibernate & use something more reliable? - totally lost now!
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG! after hours of J2EE typical painstakingly stressful messing about I found it!

If you do not specify the column attribute on the <many-to-many> line, hibernate in its wisdom bizarrely assumes a column call "elt"!!!

so for anyone else this line does it -

<many-to-many class="test.EventManyToMany" column="event_id"/>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic