-->
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: child with composite key
PostPosted: Mon Apr 05, 2004 5:22 pm 
Newbie

Joined: Fri Apr 02, 2004 4:12 pm
Posts: 12
I have a table A, B and AB. A and B each have primary keys. AB has a composite key of foreign keys to A and B. How do I model this in hibernate? So far, I've created four classes:

Code:
public class A
{
  private String aId;
  public String getAId()
  public void setAId(String);

}
public class B
{
  private String bId;
  public String getBId()
  public void setBId(String);
}
public class AB
{
  private ABKey id;
  public void setId(ABKey);
  public ABKey getId();
}
public class ABKey
{
  private A aId;
  private B bId;
  public void setA(A);
  public A getA();
  public void setB(B);
  public B getB();
}

So to model the parent-child relationship, I assume I would add a Set of type AB to A and B, i.e.:
Code:
public class A
{
  // ... see above
  private Set ab; // a Set of ABs
  public void setAb(Set);
  public Set getAb();
}

// class B would get the same

Now, do I add a property of type A and type B to AB? i.e.
Code:
public class AB
{
  // ... see above
  private A a;
  private B b;
  // get and set methods omitted
}

Or is that already handled because of the ABKey composite-id?? Can I model AB as a one-to-many or do I have to use a composite-element because it has a composite key??

RMC


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 5:45 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are you sure you dontt directly want this?
Code:
public class AB
{
  private A a;
  private B b;
}


if yes use
Code:
<composite-id>
<key-many-to-one name="a" class="A">
<key-many-to-one name="b" class="B">
</composite-id>

+ overrides equals & hashcode


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:10 pm 
Newbie

Joined: Fri Apr 02, 2004 4:12 pm
Posts: 12
delpouve wrote:
+ overrides equals & hashcode

Which class(es) override equals and hashcode? Is there a benefit over this way versus the other?

RMC


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you must overrides equals hashcode for all classes that have composite id, these classes must also implements Serializable.

Code:
Is there a benefit over this way versus the other?

[quote]Is there a benefit over this way versus the other?
[/quote]

It depends on how you want to acceed the data....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:54 pm 
Newbie

Joined: Fri Apr 02, 2004 4:12 pm
Posts: 12
delpouve wrote:
It depends on how you want to acceed the data....

Can you elaborate?


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.