例外:ejb3 中的 postgresql 表/序列不存在关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11306655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception: relation does not exist with postgresql table / sequence in ejb3
提问by App Work
I am having error when I use ejb3 and Postgres to insert data. But using raw SQL it goes fine.
使用 ejb3 和 Postgres 插入数据时出错。但是使用原始 SQL 就可以了。
My table is:
我的表是:
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 is:
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;
}
}
While calling entitmanager.persist(mail)
I get this error:
打电话时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)
在 java.lang.Thread.run(Thread.java:662) 引起:异常 [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):org.eclipse.persistence.exceptions.DatabaseException 内部异常: org.postgresql.util.PSQLException:错误:关系“vmb_mails_seq”不存在位置:16 错误代码:0 调用:选择 nextval('vmb_mails_seq') 查询:ValueReadQuery(sql="select nextval('vmb_mails_seq')") at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) 在 org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644)
回答by Erwin Brandstetter
You can simplify your DDL script with the use of a serial
data type. bigserial
in your case:
您可以使用serial
数据类型来简化 DDL 脚本。bigserial
在你的情况下:
CREATE TABLE vmb_mails (
id bigserial PRIMARY KEY,
title text,
-- more columns
);
This creates a sequence named vmb_mails_id_seq
automatically.
Also note that privileges on sequences have to be assigned separately.
这将创建一个vmb_mails_id_seq
自动命名的序列。
另请注意,序列的权限必须单独分配。
GRANT INSERT ON vmb_mails TO ..
Does not cover the sequence. Additionally you need:
不包括序列。此外,您还需要:
GRANT USAGE ON SEQUENCE vmb_mails_id_seq TO ..
However, the error message says:
但是,错误消息说:
relation "vmb_mails_seq" does not exist
关系“vmb_mails_seq”不存在
And that has some other cause. Did you actually check if the sequence is there? In the right database at the right port and the right schema?
这还有其他一些原因。你真的检查过序列是否存在吗?在正确端口和正确模式的正确数据库中?