-->
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: Multiple OneToMany Set<> of the same entity
PostPosted: Wed Jan 13, 2010 6:22 pm 
Newbie

Joined: Tue Dec 15, 2009 4:35 pm
Posts: 4
I hope the subject isn't too confusing. Keep in mind that I have no saying over the database schema. What's there is there, unfortunately.

This is the current structure that I'm trying to annotated
LINEITEM
- primaryid (pk)
- secondaryid (fk)

COMPONENT
- id (pk)
- data

The LineItem entity has
Code:
Set<Component> firstSet;
Set<Component> secondSet

I can get firstSet without any problem, but not the second, because the joining field isn't the primary key.
I have tried many annotations. How can I explicitly state what field to create the relationship on?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: Multiple OneToMany Set<> of the same entity
PostPosted: Fri Jan 15, 2010 4:39 pm 
Newbie

Joined: Tue Dec 15, 2009 4:35 pm
Posts: 4
The answer is pretty simple, but it took me 2 days to find:

Code:
public class LineItem implements Serializable, XmlSerializable {
   private String id; // our primary key
   private String otherid; // our foreign key

   private Set<Component> firstSet; // onetomany linked with primary key
   private Set<Component> secondSet; // onetomany linked with foreign key

   @Id @Column(name="FIRSTSET_ID", insertable = false, updatable = false, nullable = false)
   public String getId() { return this.id; }
   public void setId(String i) { this.id = i; }

   @OneToMany(fetch=FetchType.EAGER)
   @JoinColumn(name="ID", referencedColumnName="FIRSTSET_ID")
   public Set<ComponentData> getFirstSet() {
      return lineItemData;
   }
   public void setFirstSet(Set<Component> c) {
      this.lineItemData = lineItemData;
   }

   @Column(name="OTHERNONPKID")
   public String getOtherid() { return otherid; }
   public void setOtherid(String o) { this.otherid = o; }

   @OneToMany(fetch=FetchType.EAGER)
   @JoinColumn(name="ID", referencedColumnName="OTHERNONPKID")
   public Set<Component> getSecondSet() {
      return secondSet;
   }
   public void setSecondSet(Set<ComponentData> s) {
      this.secondSet = secondSet;
   }


The key is @JoinColumn(name="ID", referencedColumnName="FIRSTSET_ID")
In the FirstSet case, it is optional, but not in for the SecondSet


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.