webtier@glassfish.java.net

Re: [webtier] 'Attempt to modify an identity column' exception (no Hibetnate)

From: Lincoln Baxter, III <lincolnbaxter_at_gmail.com>
Date: Wed, 15 Jul 2009 08:44:31 -0400

Try using @Generated(GenerationStrategy.AUTO) on the ID field.

That may not be the exact syntax, but it's something like that.

On Wed, 2009-07-15 at 04:38 -0700, webtier_at_javadesktop.org wrote:

> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>