开发者

Hibernate duplicate primary key on restart using GenerationType.TABLE

开发者 https://www.devze.com 2022-12-29 16:25 出处:网络
We\'re running into an issue where we have Event subclasses that use GenerationType.TABLE to generate the primary key, and when we restart the servers we are getting duplicate primary key errors.

We're running into an issue where we have Event subclasses that use GenerationType.TABLE to generate the primary key, and when we restart the servers we are getting duplicate primary key errors.

We're using SQL Server and Hibernate version 3.5.1-Final.

Here's what our Hibernate annotations look like:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long eventID;

we don't specify the allocationSize so we're using the default value. Th开发者_如何学Ce hibernate sequences table does increment but it seems like on restarts it's reusing already used ID's.


Try GenerationType.AUTO or SEQUENCE. AUTO may work via hibernate magic, but SEQUENCE should create, funnily enough, a sequence in the database which it will use to get unique IDs. Which SQL Server are you using?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号