What is the Java annotation in Hibernate used to auto increment a MySQL Primary Key - @Id
Although you could use GenerationType.AUTO
, it's not a very good idea for MySQL and Hibernate 5 because it will default to the TABLE
generator which is bad for performance.
So, although [it will disable JDBC batch inserts][3], you should use IDENTITY
:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
or you can use native
identifier generator which falls back to IDENTITY
on MySQL:
@Id
@GeneratedValue(
strategy= GenerationType.AUTO,
generator="native"
)
@GenericGenerator(
name = "native",
strategy = "native"
)
private Long id;
try to use @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
take a look in this doc about Auto Generated Values