users@glassfish.java.net

Re: how to restrict persistence from db identity column insertion

From: <glassfish_at_javadesktop.org>
Date: Mon, 17 Sep 2007 14:24:47 PDT

Here is an example of what I do. The database is Derby and the column is defined as "ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY". Here is how I have my Entity annotated. Note that I made the "setId" method "protected" so it cannot be set outside of this class or subclass:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    /** Creates a new instance of PcsAgent */
    public PcsAgent() {
    }
    
    /**
     * Gets the id of this PcsAgent.
     * @return the id
     */
    public Integer getId() {
        return this.id;
    }
    
    /**
     * Sets the id of this PcsAgent to the specified value.
     * @param id the new id
     */
    protected void setId(Integer id) {
        this.id = id;
    }
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=235709