Postgresql JPA和Postgres序列预分配大小设置不正确
由于序列问题,我无法观察任何实体。我使用glssfish4、postgres9.3+JPA+EJB3和netbeans8。 以下为会议纪要:Postgresql JPA和Postgres序列预分配大小设置不正确,postgresql,jpa,ejb,sequence,glassfish-4,Postgresql,Jpa,Ejb,Sequence,Glassfish 4,由于序列问题,我无法观察任何实体。我使用glssfish4、postgres9.3+JPA+EJB3和netbeans8。 以下为会议纪要: Finest: persist() operation called on: MyUser{id=null, email=a@e.it, password=test, firstname=test, lastname=test, company=Test}. Finest: Execute query ValueRe
Finest: persist() operation called on: MyUser{id=null, email=a@e.it, password=test, firstname=test, lastname=test, company=Test}.
Finest: Execute query ValueReadQuery(sql="select nextval('mom_seq_id')")
Finest: Connection acquired from connection pool [read].
Finest: reconnecting to external connection pool
Fine: select nextval(mom_seq_id)
Finest: Connection released to connection pool [read].
Warning: Local Exception Stack:
Exception [EclipseLink-7027] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The sequence named [mom_seq_id] is setup incorrectly. Its increment does not match its pre-allocation size.
at org.eclipse.persistence.exceptions.ValidationException.sequenceSetupIncorrectly(ValidationException.java:1604)
at org.eclipse.persistence.sequencing.StandardSequence.createVector(StandardSequence.java:96)
...
Postgres上的顺序:
CREATE SEQUENCE my_seq_id
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 27
CACHE 1;
ALTER TABLE my_seq_id
OWNER TO postgres;
COMMENT ON SEQUENCE my_seq_id
IS 'Sequence for autoincrement id on MyClass';
以及我的实体摘录:
@Entity
@Table(name = "myuser")
@XmlRootElement
public class MyUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="MYSEQ",
sequenceName="my_seq_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="MYSEQ")
@Basic(optional = false)
@Column(name = "id")
private Integer id;
有人能解释出什么问题吗?
谢谢
我解决了我的问题,但我不知道为什么!我看到allocationSize()的默认值是50:
我已经将我的Postgres序列
increment\u的值从1更新到50,现在它可以工作了 出于我无法理解的原因,JPA规范选择50
作为序列生成器的默认增量
PostgreSQL默认为1
如果两者不匹配,事情就会变得糟糕,因为JPA认为它可以使用其他人认为他们已经分配的值。至少EclipseLink检测到了这一点;Hibernate只是继续愉快地尝试重用已经分配的键
如果您的顺序是:
CREATE SEQUENCE my_seq_id
INCREMENT 1
那么您的映射必须反映:
@SequenceGenerator(name="MYSEQ",
sequenceName="my_seq_id", allocationSize=1)
我强烈建议明确增量,即使您将其保留为默认值50
,并改为更改PostgreSQL序列。在以后调试时,它将保存您和其他人的理智。将增量的值从1更改为50到my Postgres序列解决了此问题。正如@unwichtich所建议的,通过@SequenceGenerator注释指定allocationSize=50属性是个好主意。如果您自己解决了问题,您应该删除问题或帖子并接受答案。最初,我将allocationSize=1属性指定到@SequenceGenerator注释中,并在注释中添加增量1我的id是Postgres序列,但它不起作用。我忘了提到我正在使用EclipseLink。
@SequenceGenerator(name="MYSEQ",
sequenceName="my_seq_id", allocationSize=1)