异常错误:ejb 3中不存在与postgresql表/序列的关系

h7appiyu  于 2022-11-04  发布在  PostgreSQL
关注(0)|答案(2)|浏览(134)

当 我 使用 ejb3 和 postgres 插入 数据 时 出现 错误 。 但是 使用 raw sql 就 可以 了 。
我 的 表 是 :

CREATE SEQUENCE vmb_mails_seq;

CREATE TABLE vmb_mails (
   ID BIGINT DEFAULT nextval('vmb_mails_seq') PRIMARY KEY,
   TITLE TEXT ,
   FROM_ADDR VARCHAR(256), 
   DATE_ARRIVED TIMESTAMP,
   BODY TEXT, 
   ENT_BY    NUMERIC(20,0),
   IS_DELETED NUMERIC(1,0),
   DELETE_DATE TIMESTAMP, 
   MOD_BY    NUMERIC(20,0),
   IS_ACTIVE NUMERIC(1,0), 
   ENT_DATE TIMESTAMP,
   MOD_DATE TIMESTAMP, 
   REACTIVE_DATE TIMESTAMP,
   INACTIVE_DATE TIMESTAMP
);
ALTER SEQUENCE vmb_mails_seq OWNED BY vmb_mails.id;

中 的 每 一 个
ejb3 实体 为 :

@Entity
@Table(name="vmb_mails")
@SequenceGenerator(name="Mails_Seq_Gen",sequenceName="vmb_mails_seq",allocationSize=1)
public class Mail implements Serializable
{
    private long id;
    private String title;
    private String fromAddr;
    private Date dateArrived;
//  private Clob body;
    private String body;

    private long entBy;
    private int isDeleted;
    private Date deleteDate;
    private long modBy;
    private int isActive;   
    private Date entDate;
    private Date modDate;
    private Date reActiveDate;
    private Date inActiveDate;

    public Mail()
    {

    }
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="Mails_Seq_Gen")
    @Column(name="ID")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Column(name="TITLE")

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Column(name="FROM_ADDR")

    public String getFromAddr() {
        return fromAddr;
    }
    public void setFromAddr(String fromAddr) {
        this.fromAddr = fromAddr;
    }
    @Column(name="DATE_ARRIVED")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDateArrived() {
        return dateArrived;
    }
    public void setDateArrived(Date dateArrived) {
        this.dateArrived = dateArrived;
    }
    @Column(name="BODY")
    public String getBody()
    {
        return body;
    }
    public void setBody(String body)
    {
        this.body = body;
    }
    /*public Clob getBody() {
        return body;
    }
    public void setBody(Clob body) {
        this.body = body;
    }*/
    @Column(name="ENT_BY")

    public long getEntBy() {
        return entBy;
    }
    public void setEntBy(long entBy) {
        this.entBy = entBy;
    }
    @Column(name="IS_DELETED")

    public int getIsDeleted() {
        return isDeleted;
    }
    public void setIsDeleted(int isDeleted) {
        this.isDeleted = isDeleted;
    }
    @Column(name="DELETE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDeleteDate() {
        return deleteDate;
    }
    public void setDeleteDate(Date deleteDate) {
        this.deleteDate = deleteDate;
    }
    @Column(name="MOD_BY")

    public long getModBy() {
        return modBy;
    }
    public void setModBy(long modBy) {
        this.modBy = modBy;
    }
    @Column(name="IS_ACTIVE")

    public int getIsActive() {
        return isActive;
    }
    public void setIsActive(int isActive) {
        this.isActive = isActive;
    }
    @Column(name="ENT_DATE")
    @Temporal(TemporalType.TIMESTAMP)

    public Date getEntDate() {
        return entDate;
    }
    public void setEntDate(Date entDate) {
        this.entDate = entDate;
    }
    @Column(name="MOD_DATE")
    @Temporal(TemporalType.TIMESTAMP)

    public Date getModDate() {
        return modDate;
    }
    public void setModDate(Date modDate) {
        this.modDate = modDate;
    }
    @Column(name="REACTIVE_DATE")
    @Temporal(TemporalType.TIMESTAMP)

    public Date getReActiveDate() {
        return reActiveDate;
    }
    public void setReActiveDate(Date reActiveDate) {
        this.reActiveDate = reActiveDate;
    }
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="INACTIVE_DATE")

    public Date getInActiveDate() {
        return inActiveDate;
    }
    public void setInActiveDate(Date inActiveDate) {
        this.inActiveDate = inActiveDate;
    }

}

格式
调用 entitmanager.persist(mail) 时 , 出现 以下 错误 :
at java.lang.Thread.run(Thread.java:662) Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: relation "vmb_mails_seq" does not exist Position: 16 Error Code: 0 Call: select nextval('vmb_mails_seq') Query: ValueReadQuery(sql="select nextval('vmb_mails_seq')") at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644)

kmpatx3s

kmpatx3s1#

您可以使用serial资料类型来简化DDL指令码。bigserial在您的案例中为:

CREATE TABLE vmb_mails (
  id bigserial PRIMARY KEY
, title text,
  -- more columns
);

这将自动创建一个名为vmb_mails_id_seq的序列(在同一模式中)。
另请注意,序列具有单独的权限。

GRANT INSERT ON vmb_mails TO ..

不包括序列。此外,您还需要:

GRANT USAGE ON SEQUENCE vmb_mails_id_seq TO ..

请参阅:

但是,错误消息显示:
关系“vmb_mails_seq”不存在
这是有其他原因的。你真的检查过序列是否在那里吗?在正确的数据库中,在正确的端口,在正确的模式中?

7gcisfzg

7gcisfzg2#

您可以尝试“如果不存在则创建序列”vmb_mails_seq;'

相关问题