Skip to main content

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

2 replies [Last post]
dziku
Offline
Joined: 2009-07-14
Points: 0

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:

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]

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:

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 also tried to remove the setter method from the User 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.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dziku
Offline
Joined: 2009-07-14
Points: 0

Thanks!
@GeneratedValue(strategy=GenerationType.IDENTITY) works.

I have one more tricky question.
The [i]status[/i] column in my database is defined:

status varchar(8) not null default 'INACTIVE'

when I try to persist:
[i]User u = new User(email, password);[/i]

I get:

[i]Column 'STATUS' cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO FX.OOF_USERS (EMAIL, PASSWORD, STATUS) VALUES (?, ?, ?)
bind => [2, 2, null][/i]

When I add @Transient to this field in the User entity class it works fine.
However, I would like to use User.setStatus(String status) later on. With the status field set to transient it has no effect.

Is it possible to make the container omit the null status value in the insert statement leaving the functionality of the setStatus method intact?

Lincoln Baxter, III

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@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@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
[att1.html]