Friday, August 14, 2015

ERROR: Sequence "HIBERNATE_SEQUENCE" not found

If you have the following with H2:

@Id
@GeneratedValue
private Long id;

you might get the following error:

Hibernate:
    call next value for hibernate_sequence
8월 15, 2015 2:49:23 오후 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 90036, SQLState: 90036
8월 15, 2015 2:49:23 오후 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
call next value for hibernate_sequence [90036-187]
ex: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement

I guess `GenerationType.AUTO` uses `GenerationType.SEQUENCE`

and H2 doesn't support it.

I tried with `GenerationType.IDENTITY` as follows:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

and it works.

UPDATED:

Actually H2 DOES support `GenerationType.SEQUENCE`

and you should do as follows:

@Entity
@SequenceGenerator(
name = "USER_SEQ_GENERATOR",
sequenceName = "USER_SEQ",
initialValue = 1, allocationSize = 1)
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ_GENERATOR")
private Long id;

...
}

No comments:

Post a Comment