加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – JPA和Postgres序列预分配大小设置不正确

发布时间:2020-12-13 15:50:38 所属栏目:百科 来源:网络整理
导读:由于序列问题,我无法保留任何实体.我使用Glssfish 4,Postgres 9.3 JPA EJB3和Netbeans 8. 在兴奋之下: Finest: persist() operation called on: MyUser{id=null,email=a@e.it,password=test,firstname=test,lastname=test,company=Test}. Finest: Execute q
由于序列问题,我无法保留任何实体.我使用Glssfish 4,Postgres 9.3 JPA EJB3和Netbeans 8.
在兴奋之下:

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:

package javax.persistence;

@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface SequenceGenerator {

    public String name();

    public String sequenceName() default "";

    public String catalog() default "";

    public String schema() default "";

    public int initialValue() default 1;

    public int allocationSize() default 50;
}

我已将我的Postgres序列increment_by值从1更新为50,现在它可以工作了!

解决方法

将INCREMENT的值从1改为50到我的Postgres序列解决了这个问题.正如@unwichtich所建议的那样,通过@SequenceGenerator注释指定allocationSize = 50属性是个好主意.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读