【问题标题】:JPA and Postgres sequence pre-allocation size setup incorrectlyJPA 和 Postgres 序列预分配大小设置不正确
【发布时间】:2014-07-21 17:58:49
【问题描述】:

由于序列问题,我无法保留任何实体。我使用 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,现在它可以工作了!

【问题讨论】:

  • 如果您自己解决了问题,您应该删除问题或发布并接受答案。

标签: postgresql jpa ejb sequence glassfish-4


【解决方案1】:

由于我无法理解的原因,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 序列。以后调试时,它会节省您和其他人的理智。

【讨论】:

  • 最初我在 @SequenceGenerator 注释中指定了 allocationSize=1 属性,在 my_seq_id Postgres 序列中具有 INCREMENT 1,但它不起作用。我忘了提到我正在使用 EclipseLink。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 2012-09-16
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多