java - Postgres Serial not-null constraint -
i have following database tables:
create table company ( id serial not null, name character varying(50), activestatus boolean, address bigint, remarks character varying(1024), revision integer ) create table company_version ( id serial not null, company_id integer, name character varying(50), revision serial not null, revision_previous integer, revision_timestamp timestamp without time zone, revision_comment character varying(100), revision_userid character varying(50) )
on company table have trigger executes after insert.
insert company_version (company_id,name,revision_previous,revision_timestamp,revision_comment,revision_userid) values (new.id, new.name, (select max(revision) company_version),current_timestamp,'inserted in application','the application') returning revision revision_param; update company set revision = revision_param id = new.id;
when insert new company, company_version table get's added should. when create new companyversion object in java, , let persist via jpa, gives me not-null constraint error on "revision" column.
org.postgresql.util.psqlexception: error: null value in column "revision" violates not-null constraint
this code try execute:
final companyversion cv = new companyversion(); cv.setcompanyid(c); cv.setname(c.getname()); cv.setrevisiontimestamp(new date()); cv.setrevisioncomment("updated in application"); cv.setrevisionuserid("the application"); cv.setrevisionprevious(this.findlatestversion(c)); this.persist(cv);
this companyversion object:
public class companyversion { private static final long serialversionuid = 1l; @id @sequencegenerator(name = "company_version_id_seq", sequencename = "company_version_id_seq", allocationsize = 1) @generatedvalue(strategy = generationtype.sequence, generator = "company_version_id_seq") @basic(optional = false) @column(name = "id") private integer id; @size(max = 50) @column(name = "name") private string name; @onetomany(mappedby = "revisionprevious", fetch = fetchtype.lazy) private list<companyversion> previousrevisionlist; @joincolumn(name = "revision_previous", referencedcolumnname = "revision") @manytoone(fetch = fetchtype.lazy) private companyversion revisionprevious; @joincolumn(name = "company_id", referencedcolumnname = "id") @manytoone(fetch = fetchtype.lazy) private company companyid; @column(name = "revision_timestamp") @temporal(temporaltype.timestamp) private date revisiontimestamp; @size(max = 100) @column(name = "revision_comment") private string revisioncomment; @size(max = 50) @column(name = "revision_userid") private string revisionuserid; @basic(optional = false) @column(name = "revision") protected integer revision; public companyversion() { } public companyversion(integer id) { this.id = id; } public companyversion(integer id, int revision) { this.id = id; this.revision = revision; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public list<companyversion> getpreviousrevisionlist() { return previousrevisionlist; } public void setpreviousrevisionlist(list<companyversion> previousrevisionlist) { this.previousrevisionlist = previousrevisionlist; } public companyversion getrevisionprevious() { return revisionprevious; } public void setrevisionprevious(companyversion revisionprevious) { this.revisionprevious = revisionprevious; } public company getcompanyid() { return companyid; } public void setcompanyid(company companyid) { this.companyid = companyid; } public date getrevisiontimestamp() { return revisiontimestamp; } public void setrevisiontimestamp(date revisiontimestamp) { this.revisiontimestamp = revisiontimestamp; } public string getrevisioncomment() { return revisioncomment; } public void setrevisioncomment(string revisioncomment) { this.revisioncomment = revisioncomment; } public string getrevisionuserid() { return revisionuserid; } public void setrevisionuserid(string revisionuserid) { this.revisionuserid = revisionuserid; } public integer getrevision() { return revision; } public void setrevision(integer revision) { this.revision = revision; } }
i've tried add @sequencegenerator
, @generatedvalue
annotations on revision, no luck far (@generatedvalue
isn't allowed on 2 fields reason).
i don't think should select latest revision, add 1 , cv.setrevision()
method in backing bean. far think it's sollution.
so how let revision
field inserted automatically?
changed jpa entity from
@basic(optional = false) @column(name = "revision") protected integer revision;
to
@basic(optional = false) @column(name = "revision", insertable = false) protected integer revision;
that did trick!
Comments
Post a Comment