-->
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.  [ 4 posts ] 
Author Message
 Post subject: Add additional primary key
PostPosted: Thu Aug 14, 2008 2:45 am 
Newbie

Joined: Thu Aug 14, 2008 2:22 am
Posts: 11
Hello,
I created this classes:

@Entity
@Table(name="Part")
@IdClass(Part.class)
public class Part extends GenericPartSpecification {
@Id
@Column(name="SerialNumber", unique=true, nullable=false)
private String serialNumber;
...
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="GenericPartSpecification")
public class GenericPartSpecification implements Comparable<GenericPartSpecification> {

@Id
@Column(name="PartNumber")
protected String partNumber;
}

The class Part inherits the primary key PartNumber from GenericPartSpecification. I want that the class Part has an additional primary key SerialNumber. The code above returns this Exception:
javax.persistence.PersistenceException: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: de.fhg.atl.javabeans.Part

Is there a solution for that?

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 8:11 am 
Newbie

Joined: Fri Jul 28, 2006 8:10 am
Posts: 5
AFAIK it is not possible to have more than one primary key. It just doesn't make any sense.

Maybe what you want to use is a composite-id (http://www.hibernate.org/hib_docs/core/ ... ompositeid)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 11:46 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
Stefon is correct, you can have only one primary key on a table (at least on all relational databases I've ever worked with). I believe what you want is a unique key and I see you have added the "unique = true" to the @Column annotation for the serial number field. Does that not enforce a unique serial number for every row in the table?

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 4:54 am 
Newbie

Joined: Thu Aug 14, 2008 2:22 am
Posts: 11
Thank you for your replies.
This is what I want to do:

CREATE TABLE GenericPartSpecification (
PartNumber VARCHAR(50) PRIMARY KEY,
Description VARCHAR(128),...)

CREATE TABLE Part (
SerialNumber VARCHAR(50) NOT NULL,
PartNumber VARCHAR(50) NOT NULL,
CreationDate DATETIME NOT NULL,
PRIMARY KEY (SerialNumber, PartNumber),
FOREIGN KEY (PartNumber) REFERENCES GenericPartSpecification (PartNumber) ON DELETE CASCADE ON UPDATE CASCADE)

I thought that would be the best to do that with inheritance. But the problem is to get a additional primary key in Part.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.