-->
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.  [ 5 posts ] 
Author Message
 Post subject: Resulting OneToMany collection contains identical objects
PostPosted: Wed Jul 09, 2008 7:26 am 
Newbie

Joined: Wed Jul 09, 2008 7:08 am
Posts: 7
Location: Sweden
Having a problem with a OneToMany-mapping. After doing a find, the collection-field with the OneToMany-mapping has the right amount of objects in it but they are all the same.

For example, if one "Utlamningsstalle" is connected to 4 different "Postnummer", i get a collection containing 4 "Postnummer" objects but they are identical (the first "Postnummer" the database returns times 4).

I've looked at the SQL hibernate executes and it is correct. However the collection is not populated the way I imagined it would.

What have I missed here?

These are my entities (w/o getters, setters etc.):


Code:
@Entity
@Table(name="UTLSTALLE")
public class Utlamningsstalle {
   
   @Id
   @Column(name="INTRID_UTLST")
   long intressentid;
   @Column(name="UTLSTID")
   String utlamningsstalleid;
   @Column(name="UTLSTBEN")
   String benamning;
   String datumfom;
   String datumtom;
   String landkod;
   String sorteringskod;
   String hanteringsstalle;
   @OneToMany(mappedBy="utlamningsstalle")
   Collection<Postnummer> postnummer;
}


Code:
@Entity
@Table(name="UTLST_POSTNR")
public class Postnummer {
   
   @Id
   @Column(name="INTRID_UTLST" )
   long intressentid;
   @Column(name="POSTNR")   
   String postnummer;   
   String datumfom;
   String datumtom;
   String osakerjn;
   
   @ManyToOne
   @JoinColumn(name="INTRID_UTLST", insertable = false, updatable = false)
   Utlamningsstalle utlamningsstalle;
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 9:20 am 
Newbie

Joined: Wed Jul 09, 2008 7:08 am
Posts: 7
Location: Sweden
Withdraw the question.

I think my mistake was that I set the Id column and ManyToOne column to be the same. This would mean it can't be a ManyToOne relationship, rather a OneToOne.

Will change the Id to be a compound-key which is the actual case.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:03 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, at the very least, thanks for the update!

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 5:36 am 
Newbie

Joined: Tue Jul 08, 2008 9:56 am
Posts: 3
I have the same problem. Could you post the mapping?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 9:27 am 
Newbie

Joined: Wed Jul 09, 2008 7:08 am
Posts: 7
Location: Sweden
After changing the Id to be a compound key it works well:

Code:
public class Postnummer {
   
   @EmbeddedId PostnummerId postnummerId;
   String datumfom;
   String datumtom;
   String osakerjn;
   
   @ManyToOne
   @JoinColumn(name="INTRID_UTLST", insertable=false, updatable=false)
   Utlamningsstalle utlamningsstalle;
   
...

}



The key:

Code:
@Embeddable
public class PostnummerId implements Serializable {

   private static final long serialVersionUID = 1721527610126830732L;

   @Column(name = "INTRID_UTLST")
   private int intressentid;

   @Column(name = "POSTNR")
   private String postnummer;

...

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.