One way is something like this:
@SequenceGenerator(sequenceName="seq_xyz", name="xyz_gen", initialValue=1,
allocationSize=1)
@Id @GeneratedValue(generator="xyz_gen")
private long id
Tim
-----Original Message-----
From: glassfish_at_javadesktop.org [mailto:glassfish_at_javadesktop.org]
Sent: Sunday, April 29, 2007 4:23 AM
To: users_at_glassfish.dev.java.net
Subject: Primary Key Problem
I am a beginner to JPA.
I have a postgreSQL database with relations having a primary key of type
"serial", ie an autogenerated sequence of integers that the database takes
care of.
Now in the entity class when i specify id, i get error when i try to run it
second time as the unique constraint of this primary key is violated.
However, if i avoid primary key then i do not get any errors since the db
automatically inserts it.
My question is:
How can I tell my JPA class that the PK is autogenerated and does not need a
value from class while inserting into the db?
###### code #####
@Entity
@Table (name="test_person")
static public class Person implements java.io.Serializable
{
@Id
/*THIS IS THE PK THAT I WANT TO LET THE JPA AUTOGENERATE TO THE DB*/
private long id;
private String lastName;
private String firstName;
protected Person() {}
public Person(String lastName, String firstName)
{
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName =
lastName; }
}
[Message sent by forum member 'ketancmaheshwari' (ketancmaheshwari)]
http://forums.java.net/jive/thread.jspa?messageID=214810
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
For additional commands, e-mail: users-help_at_glassfish.dev.java.net
- application/x-pkcs7-signature attachment: smime.p7s