-->
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: Composite PK with one ID as @GeneratedValue
PostPosted: Tue Apr 10, 2012 3:25 pm 
Newbie

Joined: Tue Apr 10, 2012 2:57 pm
Posts: 8
Hello,

I am trying to use composite PK for my persistence storage. I understand that I can either use @IdClass or @EmbeddedId for my composite primary key. I have two fields in my Entity called "recordID" and "domainID". I want recordID to be @GeneratedValue(GenerationType.AUTO) and domainID to be assigned manually. My domainID may not be unique, but the combination of those two will surely be unique to an object. IS that correct?

Also, does Spring/Hibernate stop us from using @GeneratedValue with Composite PK? Currently, what I am trying to do is:
--------------------------
PK Class

Code:
@Embeddable
public class CompoundPK implements Serializable{
//SerialVersionUID will be here

private long recordID;
private String domainID;

public CompoundKey(){
}

public CompoundKey(String domainID){
this.domainID = domainID;
}

@Column(name = "RECORD_ID")
@GeneratedValue
public long getRecordID(){
return this.recordID;
}

@Column(name = "DOMAIN_ID")
public String getDomainID(){
return this.domainID;
}

public void setRecordID(long recordID){
this.recordID = recordID;
}

public void setDomainID(String domainID){
this.domainID = domainID;
}

/**
* This is where equals, hashCode, and toString method will go
*
**/
}


------------------------------------------------
ENTITY CLASS

Code:
@Entity
@Table(name = "SDS_RECORD")
@XmlRootElement
public class Sds implements Serializable{

//SerialVersionUID
private String details;
private CompoundKey localKey;
public Sds(){
}

public Sds(String details, CompoundKey newKey){
this.details = details;
this.localKey = newKey;
}

@Column(name = "DETAILS")
public String getDetails(){
return this.details;
}

@EmbeddedId
public CompoundKey getKey(){
return this.localKey;
}

// Setters are here
// toString, equals(), hashCode, bla bla bla
}


---------------------

What I am really intersted to know is if the @GeneratedValue actually works fine with composite PK because I tried to do it with @Embeddable and @EmbeddedId but it did not increment the recordiD and therefore, the composite PK was not unique. I got a recordID as '0' and a domainID as "DOM1". I also tried to persist 3 objects where each of them had the same domainID, but expected to have three different recordIDs so that the combinations are unique to respective objects. domainID on itself should not matter whereas the combination of those two do. Could someone please help me?


Top
 Profile  
 
 Post subject: Re: Composite PK with one ID as @GeneratedValue
PostPosted: Fri Jun 08, 2012 3:04 am 
Newbie

Joined: Tue Apr 10, 2012 2:57 pm
Posts: 8
I figured the solution out....There is NO solution because the idea is that either you let hibernate manage you primary key assignment operation or you manually assign it via single or composite Ids. The idea is to ensure that a complete uniqueness is maintained when a persistence Id is generated (either auto or manual). To allow users to generate a composite key is another mean of ensuring that uniqueness is maintained when two identities are combined.


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.