Just go directly to the GenerationType.TABLE, which is most portable. It does not depend that much about database specifics, because incrementing value is done via SQL. Also I find it more suitable than AUTO, because same generation type will be used independently from database provider. You can also use it without TableGenerator, but because our goal is to have it to function exactly same way with all the databases, we are explicitly giving needed values.
In your case mappings are:
@Entity
@TableGenerator(
name="usersGenerator",
table="ID_GENERATOR",
pkColumnName="GENERATOR_KEY",
valueColumnName="GENERATOR_VALUE",
pkColumnValue="USERS_ID",
initialValue = 1,
allocationSize=1)
public class Users {
@Id
@GeneratedValue(strategy= GenerationType.TABLE,
generator = "usersGenerator")
private Integer value;
private String name;
protected Integer getValue() {
return value;
}
protected void setValue(Integer value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Same database table (ID_GENERATOR in this case) can be used by multiple table generators. If needed, for example because of type of id, same table can store multiple pk and value columns.
Name of TableGenerator is global for persistence unit, as generator names in general are. If wished, annotation can be also located to id attribute.
Possible caveat: If I remember right, some hibernate version combinations do not support initial value. That is in general case not limiting portability. It is problem only if we have to autogenerate tables and reproduce exactly same set of id values. Workaround is to manually insert initial value after table is constructed.