-->
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: generate composite key by using-1 of keys as sequence number
PostPosted: Tue May 27, 2008 11:21 pm 
Newbie

Joined: Tue May 27, 2008 11:14 pm
Posts: 1
please help me how to generate composite key by using one of keys as a sequence number(which increments 1,2,3...). Other key I'm getting the value from Browser side(Employer ID)

I'm thinking we need to write one user class to generate this composite key, please help me how to generate composite key by using 2 keys-Employer ID, CallerSeqNo.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 9:42 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
There really is no mystery when it comes to creating compoung primary keys with Hibernate. There are a few compound primary key strategies, the simplest is to just create an @Embeddable class with your keys defined:

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


This example comes from this free tutorial:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=15usingcompoundprimarykeys


Then, just reference the @Embeddable class in your entity class. Here's the Interest class that uses the compound primary key. (Get it? Compound Interest???) :)

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


Image

Then, you just code like normal:



Code:
  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();
  }



Image

There are other strategies, like the @IdClass and @EmbeddableId as well, that you can look at. Here's a full tutorial on Hibernate Compound Primary Keys and full Hibernate3 example code:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=15usingcompoundprimarykeys


[/code]

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