-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-to-many bi-directional relationship
PostPosted: Sat Jul 14, 2007 10:49 am 
Newbie

Joined: Mon Jul 09, 2007 9:32 am
Posts: 17
I am trying Many-to-many bi-directional relationship, but failed.

Code:
Event e = new Event();
      e.setEventId(1);
      this.getHibernateTemplate().save(e);      
      
      Speaker s1 = new Speaker();
      s1.setSpeakerId(1);
      s1.setSpeakerName("s1");
      this.getHibernateTemplate().save(s1);      
      
      Speaker s2 = new Speaker();
      s2.setSpeakerId(2);
      s2.setSpeakerName("s2");
      this.getHibernateTemplate().save(s2);      
      
      List<Speaker> list = new ArrayList<Speaker>();
      list.add(s1);
      list.add(s2);
      e.setSpeakerList(list);
      this.getHibernateTemplate().update(e);
      
      DetachedCriteria criteria = DetachedCriteria.forClass(Speaker.class); 
      criteria.add(Restrictions.eq("speakerId", new Long(1)));
      
      List results = this.getHibernateTemplate().findByCriteria(criteria);
      Speaker g = (Speaker)results.get(0);
      System.out.println(g);

/////////////
<class name="data.Event" table="mts_event" >
       <cache usage="read-write"/>       
<id name="eventId" column="eventId" unsaved-value="-1">
            <generator class="assigned"/>
        </id>       
        <property name="eventName"/>        
        <list name="speakerList" table="mts_event_speaker">
           <key column="eventId"/>
           <index column="idx"/>
           <many-to-many column="speakerId" class="data.Speaker"/>
        </list>
    </class>
   
    <class name="data.Speaker" table="mts_speaker" >
       <cache usage="read-write"/>       
        <id name="speakerId" column="speakerId" unsaved-value="-1">
            <generator class="assigned"/>
        </id>       
        <property name="speakerName"/>        
        <list name="eventList" table="mts_event_speaker">
           <key column="speakerId"/>
           <index column="idx"/>
           <many-to-many column="eventId" class="data.Event"/>
        </list>
    </class>


Now the relationship data is saved into mts_event_speaker. Event and Speaker are saved.

But I want to load Speaker g. I think g should have not-null eventList, because the relationship data is in database
Code:
Hibernate: insert into mts_event_speaker (eventId, idx, speakerId) values (?, ?, ?)
2007-07-13 14:16:03,027 DEBUG [org.hibernate.type.LongType] - binding '1' to parameter: 1
2007-07-13 14:16:03,027 DEBUG [org.hibernate.type.IntegerType] - binding '1' to parameter: 2
2007-07-13 14:16:03,027 DEBUG [org.hibernate.type.LongType] - binding '2' to parameter: 3
2007-07-13 14:16:03,027 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] - done inserting collection: 2 rows inserted
2007-07-13 14:16:03,027 DEBUG [org.hibernate.jdbc.AbstractBatcher] - Executing batch size: 2
2007-07-13 14:16:03,043 DEBUG [org.hibernate.jdbc.Expectations] - success of batch update unknown: 0
2007-07-13 14:16:03,043 DEBUG [org.hibernate.jdbc.Expectations] - success of batch update unknown: 1
2007-07-13 14:16:03,043 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-07-13 14:16:03,043 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2007-07-13 14:16:03,043 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush end
2007-07-13 14:16:03,043 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - post flush
2007-07-13 14:16:03,058 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2007-07-13 14:16:03,058 DEBUG [org.hibernate.SQL] - select this_.speakerId as speakerId8_0_, this_.speakerName as speakerN2_8_0_ from mts_speaker this_ where this_.speakerId=?
Hibernate: select this_.speakerId as speakerId8_0_, this_.speakerName as speakerN2_8_0_ from mts_speaker this_ where this_.speakerId=?
2007-07-13 14:16:03,058 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2007-07-13 14:16:03,058 DEBUG [org.hibernate.type.LongType] - binding '1' to parameter: 1
2007-07-13 14:16:03,105 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
2007-07-13 14:16:03,105 DEBUG [org.hibernate.loader.Loader] - processing result set
2007-07-13 14:16:03,105 DEBUG [org.hibernate.loader.Loader] - result set row: 0
2007-07-13 14:16:03,105 DEBUG [org.hibernate.type.LongType] - returning '1' as column: speakerId8_0_
2007-07-13 14:16:03,105 DEBUG [org.hibernate.loader.Loader] - result row: EntityKey[data.Speaker#1]
2007-07-13 14:16:03,105 DEBUG [org.hibernate.loader.Loader] - done processing result set (1 rows)
2007-07-13 14:16:03,105 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
2007-07-13 14:16:03,105 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-07-13 14:16:03,105 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2007-07-13 14:16:03,121 DEBUG [org.hibernate.loader.Loader] - total objects hydrated: 0
2007-07-13 14:16:03,121 DEBUG [org.hibernate.engine.StatefulPersistenceContext] - initializing non-lazy collections
2007-07-13 14:16:03,121 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Not closing pre-bound Hibernate Session after HibernateTemplate
data.Speaker@dab859[speakerId=1,speakerName=s1,eventList=<null>]

As you see, the Speaker has null eventList.

Weird thing is, after I restart server, it will load eventList for Speaker. I am using OSCache. query-cache and second-level cache are on.

Any idea ?

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.