java - ActiveJDBC: save() generates insert only for auto-generated primary key? -
i started using activejdbc. have following table (postgresql)
create table users ( id uuid primary key, email text unique not null, password text, <some more nullable columns> created_at timestamp time zone not null default (now() @ time zone 'utc'), updated_at timestamp time zone );
as can see, primary key of type uuid , has no auto-generate value of kind.
this user class models table :
public class user extends model { public user() { setid(uuid.randomuuid()); // sets value primary key } ... }
this attempt create new row , insert :
user u = new user(); system.out.println(u.saveit());
actually, expected insert fail, since did not set value mandatory email column. however, got false return value. when turned on logging, saw framework generated update sql instead of insert:
[main] info org.javalite.activejdbc.db - query: "update users set email = ?, ..., updated_at = ? id = ?", parameters: <null>, <null>, <null>, <null>, <null>, <null>, <null>, <2016-01-07 17:30:46.025>, <0621fbdb-5b95-4ee7-a474-8ee9165e2982>, took: 1 milliseconds
so looked @ save()
method inside org.javalite.activejdbc.model
class , saw piece of code:
if (getid() == null) { result = insert(); } else { result = update(); }
does mean id column has empty in order insert generated ? if true unacceptable, must missing something.
@sharonbn, please, see documentation page: http://javalite.io/surrogate_primary_keys. activejdbc depends on autogenerated ids. if id == null, frameworks assumes new record, , generates insert statement. if non-null, assumed record exists, , generates update.
in case, need explicitly call user.insert()
instead of user.saveit()
. 'expert' mode in cases when developers want in control of id management. further, method user.update()
private. insert new record, calling
user.insert();
and updates:
user.save(); // or: user.saveit();
, depending on want.
Comments
Post a Comment