2

我在 postgres 和 jpa 中的序列有问题

Caused by: javax.persistence.EntityExistsException:
Exception Description: The sequence named [shp_users_seq] is setup incorrectly.  Its increment does not match its pre-allocation size.
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:443)
        at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:269)
        at base.data.provider.beans.session.DAOImpl.createUser(DAOImpl.java:18)
4

2 回答 2

3

规则是:序列的增量大小是 jpa 中的 allocationSize 的值:我有这个设置:这是错误的:

@SequenceGenerator(name = "User_Seq_Gen", 
                   sequenceName = "shp_users_seq", allocationSize=999)

据此更正:

@SequenceGenerator(name = "User_Seq_Gen", 
                   sequenceName = "shp_users_seq" ,allocationSize=1)

因为序列的增量大小为 1:

 shopper=> \d shp_users_seq;
        Sequence "public.shp_users_seq"
    Column     |  Type   |        Value
---------------+---------+---------------------
 sequence_name | name    | shp_users_seq
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | t
于 2012-02-29T05:18:15.493 回答
0

尝试将序列的起始值设置为至少与您用于@SequenceGenerator注释的属性allocationSize的大小相同。

 CREATE SEQUENCE seq_name
 ...
 START WITH 100;

有关详细信息,请参阅http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg03461.html

于 2012-02-28T12:58:24.880 回答