-->
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.  [ 11 posts ] 
Author Message
 Post subject: many-to-many but still only one attached
PostPosted: Wed Feb 07, 2007 3:50 pm 
Newbie

Joined: Wed Feb 07, 2007 3:13 pm
Posts: 4
hi

i have a many-to-many association between list and candidate:

Code:
      <set name="candidates"
         table="LIST_CANDIDATE">
         <key column="LIST_ID" />
         <many-to-many class="Candidate"  column="CANDIDATE_ID"/>
      </set>


now when i make a list and add 10 times the same candidate.

but then when i want to get the candidates of that list:

Code:
      Set<Candidate> candidates = list.getCandidates();
      for (Candidate candidate : candidates) {


there is only one candidate in the list. but in the sql table list_candidate there are the 10 entrys list_id and the corresponding candidate_id...

what have i done wrong or forgotten??

thx 4 help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 10:56 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I would have expected hibernate to throw an exception because you are adding duplicate rows.

Do you also have a similar mapping in Candidate.hbm.xml that maps candidates to list?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 5:57 am 
Newbie

Joined: Wed Feb 07, 2007 3:13 pm
Posts: 4
i tried now the same set relation in the candidate.hbm.xml:

Code:
      <set name="voteLists"
         table="LIST_CANDIDATE">
         <key column="CANDIDATE_ID" />
         <many-to-many class="VoteList" column="LIST_ID"/>
      </set>


but it still doesnt work...

the sets where from the beginning in both definitionclasses with the corresponding getter and setter and a add() method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 6:23 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
Did you try a refresh of the object after storing it?
I've discovered this is sometimes needed (though that was in a different context).

Or maybe it's indeed smart enough to detect you are trying to attach duplicates and silently ignoring that (I would expect at least some logging in that case, try pumping up your logging levels to see if something happens).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 11:07 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Can you post the code where you are creating and adding those candidates?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:35 pm 
Newbie

Joined: Wed Feb 07, 2007 3:13 pm
Posts: 4
Code:
public void saveList(){
      SessionFactory sf = ApplicationContext.getInstance().getSessionFactory();
      Session session = sf.openSession();
      session.beginTransaction();
      
      VoteList list = new VoteList();
      if (getCandidate(can[0]) != null){
         list.addCandidate(getCandidate(can[0]));
         addOneVoteToCandidate(can[0]);
      }
      if (getCandidate(can[1]) != null){
         list.addCandidate(getCandidate(can[1]));
         addOneVoteToCandidate(can[1]);
      }
      if (getCandidate(can[2]) != null){
         list.addCandidate(getCandidate(can[2]));
         addOneVoteToCandidate(can[2]);
      }
      if (getCandidate(can[3]) != null) {
         list.addCandidate(getCandidate(can[3]));
         addOneVoteToCandidate(can[3]);
      }
      if (getCandidate(can[4]) != null) {
         list.addCandidate(getCandidate(can[4]));
         addOneVoteToCandidate(can[4]);
      }
      if (getCandidate(can[5]) != null) {
         list.addCandidate(getCandidate(can[5]));
         addOneVoteToCandidate(can[5]);
      }
      if (getCandidate(can[6]) != null) {
         list.addCandidate(getCandidate(can[6]));
         addOneVoteToCandidate(can[6]);
      }
      if (getCandidate(can[7]) != null) {
         list.addCandidate(getCandidate(can[7]));
         addOneVoteToCandidate(can[7]);
      }
      if (getCandidate(can[8]) != null) {
         list.addCandidate(getCandidate(can[8]));
         addOneVoteToCandidate(can[8]);
      }
      if (getCandidate(can[9]) != null) {
         list.addCandidate(getCandidate(can[9]));
         addOneVoteToCandidate(can[9]);
      }
      
      if(partei != null){
         list.setPartei(partei);
      }
      list.setUser("1");
      
      session.saveOrUpdate(list);
      session.getTransaction().commit();

   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:39 pm 
Newbie

Joined: Wed Feb 07, 2007 3:13 pm
Posts: 4
i think the problem could be that i use Sets and the problems of sets is, they delete dublicates automaticly. but no theres the problem in my voteList.hbm.xml i defined the many-to-many relationship as a set...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 1:12 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
There you go. Yes. Set does not allow duplicate entries. i.e. the add() method returns false.

Although I'm still surprised that Hibernate actually lets you add the duplicate entries. It should not.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 5:36 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
If he's using a Set to back his Collection and he's trying to add the same item several times, Hibernate would never notice that attempt because the Set would intercept it before Hibernate comes into play.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 7:49 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I think in the original post he said he ends up with 10 entries in the table in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 10, 2007 2:32 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
no, it says he intended to insert 10 records but never that those records are actually being created.


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

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.