users@glassfish.java.net

Primary Key Problem

From: <glassfish_at_javadesktop.org>
Date: Sun, 29 Apr 2007 01:22:49 PDT

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