-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToMany resulting Correct and Incorrect Associations
PostPosted: Tue Jun 02, 2009 11:31 pm 
Newbie

Joined: Tue Feb 03, 2009 7:26 pm
Posts: 10
I have JPA annotated entity association as below

Code:
@Entity
@Table(name = "COLLECTIONSITE")
public class CollectionSite implements Serializable {
   @OneToMany(mappedBy = "site", fetch = FetchType.LAZY)
   @Where(clause = "EFF_START_DATE <> EFF_END_DATE")
   private Collection<MiscData> miscData;
}

@Entity
@Table(name = "MISC_DATA")
public class MiscData  implements Serializable{
   @ManyToOne
   @JoinColumn(name = "SITE")
   private CollectionSite site;
}


This is resulting the following associations at different app flows. Thanks to Eclipse's Debug Variable panel's Copy Variables feature.

<!-- Correct Association -->
this CollectionSite (id=229)
miscData PersistentBag (id=230)
bag ArrayList<E> (id=271)
elementData Object[10] (id=273)
[0] MiscData (id=274)
site CollectionSite (id=229)
[1] MiscData (id=275)
site CollectionSite (id=229)
[2] MiscData (id=276)
site CollectionSite (id=229)
[3] MiscData (id=277)
site CollectionSite (id=229)
modCount 4
size 4

<!-- InCorrect Association -->
this CollectionSite (id=353)
miscData PersistentBag (id=354)
bag ArrayList<E> (id=357)
elementData Object[10] (id=372)
[0] MiscData (id=373)
site CollectionSite (id=379)
[1] MiscData (id=374)
site CollectionSite (id=379)
[2] MiscData (id=375)
site CollectionSite (id=353)
modCount 3
size 3

Any thoughts on how the parent association is going wrong ?


Top
 Profile  
 
 Post subject: Re: OneToMany resulting Correct and Incorrect Associations
PostPosted: Wed Jun 03, 2009 3:42 am 
Newbie

Joined: Tue Feb 03, 2009 7:26 pm
Posts: 10
This was solved by using IdClass as below for the associated class.
Code:
@Entity
@Table(name = "MISC_DATA")
@IdClass(value = MiscData.IdClass.class)
public class MiscData implements Serializable{
   public static class IdClass implements Serializable{
      @Column(name = "SITE")
      public String siteId;
      @Column(name = "TYPE")
      public String type;
      @Column(name = "EFF_START_DATE")
      public Date startDate;
      @Column(name = "EFF_END_DATE")
      public Date endDate;
   }

   @Id
   @ManyToOne
   @JoinColumn(name = "SITE")
   private CollectionSite site;

   @Id
   @Column(name = "TYPE")
   private String type;
   
   @Id
   @Column(name = "EFF_START_DATE")
   private Date startDate = getInfinite();

   @Id
   @Column(name = "EFF_END_DATE")
   private Date endDate = getInfinite();

   @Column(name = "DATA")
   private String data;
   
   @Basic
   @Column(name= "site", insertable=false, updatable=false)
   public String siteId;
}

There was stranded @Id in the class which caused the error.


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