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?
7 Answers
@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.
-
my id is of type string. what will i set to it. Because I get this error. Caused by: javax.el.ELException: org.hibernate.exception.SQLGrammarException: could not get next sequence value – cedric Jan 06 '10 at 08:11
-
4autoincrement means it is a number which is incremented. A String cannot be incremented. Make the column int – Bozho Jan 06 '10 at 08:36
-
myid column in the database is of type number. And i already changed my id in my pojo to int. i get the error sequence does not exist – cedric Jan 06 '10 at 09:22
-
1you didn't say what your database is. Try setting it to IDENTITY, rathen than AUTO. – Bozho Jan 06 '10 at 09:28
-
imusing oracle database. when i set it to identity it gives this error Caused by: org.hibernate.MappingException: Dialect does not support identity key generation Here's my dialect
org.hibernate.dialect.Oracle10gDialect – cedric Jan 06 '10 at 09:38 -
2For Oracle, SEQUENCE is the closest thing to autoincrement. You have to create the sequence ahead of time unless you're letting Hibernate generate your schema. If you think you might support multiple databases at some point, use TABLE. – Brian Deterling Jan 06 '10 at 14:16
-
Well, in fact, I'm also using Oracle with AUTO and it works like charm – Bozho Jan 06 '10 at 14:46
-
2Don't use identity, Oracle does not support identity, it supports sequence. – JuanZe Jan 06 '10 at 17:23
-
This worked for me... @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="gen_USERS") @SequenceGenerator(name="gen_USERS", sequenceName = "USER_SEQ")@Column(name="ID") – cedric Jan 12 '10 at 05:50
-
@Bozho Is there any strategy which gives the next ID available to the added object? What I mean by that is if I delete the last object from a table with ID 5, will the new added element get the ID 5? I've used almost all strategies and it always gives it the 6 ID. Is there any way to make it happen? – आनंद Jun 12 '17 at 03:57
Do it as follows :-
@Id
@GenericGenerator(name="kaugen" , strategy="increment")
@GeneratedValue(generator="kaugen")
@Column(name="proj_id")
public Integer getId() {
return id;
}
You can use any arbitrary name instead of kaugen. It worked well, I could see below queries on console
Hibernate: select max(proj_id) from javaproj
Hibernate: insert into javaproj (AUTH_email, AUTH_firstName, AUTH_lastName, projname, proj_id) values (?, ?, ?, ?, ?)

- 6,439
- 13
- 50
- 76
-
This one works for me. But it don't let me set ID value. I tried to setID with integers or int, but it uses max anytime. What should I do? – desudesudesu Jun 11 '12 at 08:11
-
use @GenericGenerator(name="incrementId",strategy="assigned") @GeneratedValue(generator="incrementId").It will allow you to set id by your own.But if you not pass id then it will be 0. – Ravi Kant Feb 04 '14 at 11:49
-
@Kaushik Lele Is strategy="increment" hibernate inbuilt increment stratergy ? Is it comes under which of these SEQUENCE,IDENTITY,AUTO,TABLE ? – Nagappa L M May 02 '14 at 06:31
-
How about 700 millions records in table? This could be the issue without index I think – user2171669 Jan 28 '15 at 17:07
FYI
Using netbeans New Entity Classes from Database with a mysql *auto_increment* column, creates you an attribute with the following annotations:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
@NotNull
private Integer id;
This was getting me the same an error saying the column must not be null, so i simply removed the @NotNull anotation leaving the attribute null, and it works!

- 1,556
- 2
- 16
- 26
Hibernate defines five types of identifier generation strategies:
AUTO - either identity column, sequence or table depending on the underlying DB
TABLE - table holding the id
IDENTITY - identity column
SEQUENCE - sequence
identity copy – the identity is copied from another entity
Example using Table
@Id
@GeneratedValue(strategy=GenerationType.TABLE , generator="employee_generator")
@TableGenerator(name="employee_generator",
table="pk_table",
pkColumnName="name",
valueColumnName="value",
allocationSize=100)
@Column(name="employee_id")
private Long employeeId;
for more details, check the link.

- 393
- 3
- 6
If you have a numeric column that you want to auto-increment, it might be an option to set columnDefinition
directly. This has the advantage, that the schema auto-generates the value even if it is used without hibernate. This might make your code db-specific though:
import javax.persistence.Column;
@Column(columnDefinition = "serial") // PostgreSQL
@Column(columnDefinition = "integer auto_increment") // MySql

- 6,936
- 2
- 49
- 58
In case anyone "bumps" in this SO question in search for strategies for Informix table when PK is type Serial.
I have found that this works...as an example.
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "special_serial_pk")
private Integer special_serial_pk;
For this to work make sure when you do session.SaveOrUpdate you pass the value for the column special_serial_pk NULL .
In my case i do an HTML POST with JSON like so...
{
"special_serial_pk": null, //<-- Field to be incremented
"specialcolumn1": 1,
"specialcolumn2": "I love to code",
"specialcolumn3": true
}

- 948
- 1
- 14
- 36
Using netbeans New Entity Classes from Database with a mysql auto_increment column, creates you an attribute with the following hibernate.hbm.xml: id is auto increment

- 1
- 1