Reputation: 666
I have the 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 the company table I have a trigger that executes after the insert.
INSERT INTO company_version (company_id,name,revision_previous,revision_timestamp,revision_comment,revision_userid)
VALUES (NEW.id, NEW.name, (SELECT MAX(revision) FROM company_version),CURRENT_TIMESTAMP,'Inserted in application','The application')
RETURNING revision into revision_param;
UPDATE company SET revision = revision_param WHERE id = NEW.id;
When I insert a new company, the company_version table get's added like it should. But when I create a new CompanyVersion object in Java, and let it persist via JPA, it gives me a not-null constraint error on the "revision" column.
org.postgresql.util.PSQLException: ERROR: null value in column "revision" violates not-null constraint
This is the code I try to 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 is my 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 already tried to add the @SequenceGenerator
and @GeneratedValue
annotations on the revision, but no luck so far (@GeneratedValue
isn't allowed on 2 fields for some reason).
I don't think I should select the latest revision, add one and do the cv.setRevision()
method in my backing bean. But so far I think it's the only sollution.
So how do I let the revision
field get inserted automatically?
Upvotes: 4
Views: 3151
Reputation: 666
Changed my 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 the trick!
Upvotes: 3