webtier@glassfish.java.net

'Attempt to modify an identity column' exception (no Hibetnate)

From: <webtier_at_javadesktop.org>
Date: Wed, 15 Jul 2009 04:38:26 PDT

Hi!

My JavaDB table contains a column:

...
id int generated always as identity primary key
...

My entity class for this table is:

...
@Entity
@Table(name = "FX.OOF_USERS")
public class User implements Serializable {

    @Id
    private int id;
    private String email;
    private String password;
    private String status;

    public User() {
    }

    public User(String email, String password) {
        this.email = email;
        this.password = password;
    }
...

When I'm trying to call:
...
User u = new User(email, password);
em.persist(u);
em.flush();
...

I'm getting:

[i]Internal Exception: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'.
Error Code: -1
Call: INSERT INTO FX.OOF_USERS (ID, EMAIL, PASSWORD, STATUS) VALUES (?, ?, ?, ?)
        bind => [0, 1, 1, null][/i]

which is OK since the ID column is an identity column.

I tried to make it transient:

...
@Id
private transient int id;
...

but it didn't work:


[i]Exception Description: Mapping annotations cannot be applied to fields or properties that are transient or have a @Transient specified. [private transient int database.User.id] is in violation of this restriction.[/i]

I also tried to remove the setter method from the [i]User[/i] entity class:

...
public void setId(int id) {
  this.id = id;
}
...

but, as I thought, it didn't help either (I still got the 'Attempt to modify...' exception).

Is there any way to make it work?
Of cource I could try to walk around this problem using triggers or creating
a table to store id values instead of identiy column, but that's not the point.
[Message sent by forum member 'dziku' (dziku)]

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