-->
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: Mapping Set with Composite Key
PostPosted: Mon Jul 28, 2008 5:11 am 
Newbie

Joined: Mon Jul 28, 2008 4:48 am
Posts: 3
Hi. Iam new to hibernate and Iam trying to map the following relation:

C_Article
* ID_ART (PK)
..........

C_Article_Type
* ID_TYPE (PK)
..........

T_Article_Price
* ID_ART (PK, FK)
* ID_TYPE (PK.,FK)
PRICE


-C_Article and C_Price_Type are tables containing, as their name imply, information about article and type of price.
-T_Price is a table that contains the list of prices given a certain artice. Notice that the primary key is a composite key of ID_ART and ID_PRICE_TYPE).

in this way I need a set in Article that contains all the prices in T_Price.

How can i map this? Any hint or suggestion is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 7:02 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
On first look, this simply looks like a question about compound primary keys, right? Composite primary keys are no mystery in Hibernate, and there are a few ways to implement them, includinging IdClass and EmbeddedId. Personally, I just use the Embeddable annotation if I can.


Hibernate3 Tutorial on Using Composite Primary Keys: Mapping Compound Id Keys

So, with a compound key like this:

Image

You create an embeddable object, as so:

Code:
package com.examscam.mappings;
import javax.persistence.Embeddable;
/* First Iteration of the CompoundKey Class */
@Embeddable
public class CompoundKey implements
                             java.io.Serializable{
  private Long userId;
  private Long bankId;
  public CompoundKey() {}
  public CompoundKey(Long user, Long bank) {
    userId = user;
    bankId = bank;
  }

  public Long getBankId() {return bankId;}
  public void setBankId(Long bankId) {
    this.bankId = bankId;
  }
  public Long getUserId() {return userId;}
  public void setUserId(Long userId) {
    this.userId = userId;
  }
}


Hibernate3 Tutorial on Using Composite Primary Keys: Mapping Compound Id Keys


Then you just use it in your class:

Code:
package com.examscam.mappings;
import javax.persistence.*; import org.hibernate.Session;
import com.examscam.HibernateUtil;
@Entity
public class Interest {

  private CompoundKey id;
  private double rate;

  @Id
  public CompoundKey getId() {return id;}
  public void setId(CompoundKey id) {this.id=id;}
  public double getRate() {return rate;}
  public void setRate(double rate) {this.rate=rate;}

  public static void main(String args[]) {
    Interest rate = new Interest();
    rate.setRate(18.5);

    Long wayne=new Long(99); Long mario=new Long(88);
    CompoundKey key = new CompoundKey(wayne, mario);
    rate.setId(key);

    HibernateUtil.recreateDatabase();
    Session session = HibernateUtil.beginTransaction();
    session.save(rate);
    HibernateUtil.commitTransaction();
  }
}


Here's the full tutorial and sample Java code from my site:

Hibernate3 Tutorial on Using Composite Primary Keys: Mapping Compound Id Keys

It's just that easy!

_________________
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: Compound Key
PostPosted: Mon Jul 28, 2008 3:08 pm 
Newbie

Joined: Mon Jul 28, 2008 4:48 am
Posts: 3
Thank you, i'll take a look to the example!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 6:21 pm 
Newbie

Joined: Thu Jul 24, 2008 2:59 pm
Posts: 5
Is there a way that i can do this without annotations?


Top
 Profile  
 
 Post subject: XML mapping
PostPosted: Sun Aug 03, 2008 1:10 am 
Newbie

Joined: Mon Jul 28, 2008 4:48 am
Posts: 3
Sorry for the lateness, I figured out a way of doing that kind of mapping although iam not quite sure it's the correct way.

You have to create a single class that will represent the Composite Identifier and do a mapping like this:

<class name="Price" table="t_price" >
<composite-id name="priceID">
<key-many-to-one name="acticle" class="Article' column="id_artc"/>
<key-many-to-one name="type" class="PriceType" column="id_artc_tipo_precio"/>
</composite-id>
<property name="price" type="java.lang.Float" column="Price"/>
</class>

in the example, priceID will be the identifier class. This has to implement the Serializable interface and override the has to override the equals and hashCode methods.

public class PriceIdentifier implements Serializable {
private Article article;
private PriceType type;
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((article == null) ? 0 : article.hashCode());
result = prime * result
+ ((type == null) ? 0 : type.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if(!(obj instanceof PriceIdentifier)) return false;
PriceIdentifier other = (PriceIdentifier) obj;
if (article == null) {
if (other.article != null)
return false;
} else if (!article.equals(other.article))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}

Your Price class will have attributes for the identifier and the price value.

*******
I wish i could find easier sources for Hibernate.


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.