我有以下数据库表:

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)
)

在公司表上,我有一个在插入后执行的触发器。
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;

当我插入新公司时,会像应添加的那样添加company_version表。但是,当我用Java创建一个新的CompanyVersion对象并通过JPA使其持久化时,它在“修订”列上给了我一个非空约束错误。
org.postgresql.util.PSQLException: ERROR: null value in column "revision" violates not-null constraint

这是我尝试执行的代码:
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);

这是我的CompanyVersion对象:
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;
    }
}

我已经尝试在修订版中添加@SequenceGenerator@GeneratedValue批注,但到目前为止还算运气不佳(出于某些原因,不允许在2个字段上使用@GeneratedValue)。

我不认为我应该选择最新的修订版,在我的支持bean中添加一个修订版并执行cv.setRevision()方法。但是到目前为止,我认为这是唯一的解决方案。

那么,如何让revision字段自动插入?

最佳答案

我的JPA实体从

@Basic(optional = false)
@Column(name = "revision")
protected Integer revision;


@Basic(optional = false)
@Column(name = "revision", insertable = false)
protected Integer revision;

做到了!

09-26 05:24