Hibernate Auto Increment ID

auto-increment, hibernate, java

Solution

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

and you leave it `null` (`0`) when persisting. (`null` if you use the `Integer` / `Long` wrappers)

In some cases the `AUTO` strategy is resolved to `SEQUENCE` rathen than to `IDENTITY` or `TABLE`, so you might want to manually set it to `IDENTITY` or `TABLE` (depending on the underlying database).

It seems `SEQUENCE` + specifying the sequence name worked for you.

Problem

I have a j2ee application using hibernate with annotation. How do I annotate the Id field in my pojo class to set it as auto increment or auto generated. and in adding the bean do I leave that field in my bean null?

Original source