gpt4 book ai didi

java - Postgres 串行非空约束

转载 作者:行者123 更新时间:2023-11-29 12:04:46 26 4
gpt4 key购买 nike

我有以下数据库表:

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 注释,但到目前为止还没有成功 (@GeneratedValue由于某些原因不允许在 2 个字段上使用)。

我认为我不应该选择最新修订版、添加一个修订版并在我的支持 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;

成功了!

关于java - Postgres 串行非空约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com