webtier@glassfish.java.net

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

From: <webtier_at_javadesktop.org>
Date: Wed, 15 Jul 2009 14:54:42 PDT

The table:

create table oof_users
(
id int generated always as identity primary key,
email varchar(30) not null,
password varchar(20) not null,
status varchar(8) default 'INACTIVE' not null
);

The entity:

public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String email;
    private String password;
    @Column(name = "STATUS",
            insertable = false,
            columnDefinition="varchar(8) default 'INACTIVE' not null")
    private String status;
...

Here's what I want to achieve.
I want to enable a user to register given his email and password. Then the user would have to confirm their registration by visiting a link sent via email.
Following this logic the first step would be to only insert an email and a password into the database with a default status 'INACTIVE'. Visiting the link I mentioned above would set the status to 'ACTIVE'. That's why my [i]User[/i] entity constructor takes only an email and a password parameter assuming the database will insert the default 'INACTIVE' value for the [i]status[/i] column. I don't want to insert NULL. I want to make the User constructor insert statement look like this:

INSERT INTO OOF_USERS(email, password) values...

I can do that by setting the [i]status[/i] column transient, but then I won't be able to call setStatus() method to set an ACTIVE status while confirming the registration.
I believed that setting insertable=false would solve the problem, but it didn't.

So, my question is:
How to make a constuctor insert statement like this:

INSERT INTO OOF_USERS(email, password) values...

assuming:

create table oof_users
(
id int generated always as identity primary key,
email varchar(30) not null,
password varchar(20) not null,
status varchar(8) default 'INACTIVE' not null
);

public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String email;
    private String password;
    @Column(name = "STATUS",
            insertable = false,
            columnDefinition="varchar(8) default 'INACTIVE' not null")
    private String status;

public User(String email, String password) {
  this.email = email;
  this.password = password;
}
...
[Message sent by forum member 'dziku' (dziku)]

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