-->
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: how implement composite primary key in hibernate
PostPosted: Fri Jul 18, 2008 6:26 am 
Newbie

Joined: Thu Jul 17, 2008 5:29 am
Posts: 3
How implement composite primary key in hibernate. Show me with sample code.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 6:38 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
Try reading the docs, e.g. this one.

Good Luck,
Frank


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 6:41 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
sorry, duplicate post


Last edited by AlmGhandi on Fri Jul 18, 2008 6:45 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 6:44 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
sorry, duplicate post


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 19, 2008 9:43 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Take a look at this link for help with creating composite compound primary keys with your entities:

How to create Composite Keys in Hibernate: A Tutorial

The easiest way is to use the @Embeddable tag:

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;
  }
}


Then you just plug it in your code.

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;
  }
}


Other options are to use @EmbeddedId or @IdClass

Check out my signature links for more free tutorials on using Hibernate3 in a variety of mapping and persistence scenarios.

_________________
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  
 
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.