100

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 7

187
@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.

Sign up to request clarification or add additional context in comments.

10 Comments

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
autoincrement means it is a number which is incremented. A String cannot be incremented. Make the column int
you didn't say what your database is. Try setting it to IDENTITY, rathen than AUTO.
For 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.
Don't use identity, Oracle does not support identity, it supports sequence.
|
36

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 (?, ?, ?, ?, ?)

4 Comments

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?
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.
@Kaushik Lele Is strategy="increment" hibernate inbuilt increment stratergy ? Is it comes under which of these SEQUENCE,IDENTITY,AUTO,TABLE ?
How about 700 millions records in table? This could be the issue without index I think
14

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!

Comments

10

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.

Comments

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

2 Comments

MySQL is @Column(columnDefinition = "integer auto_increment")
Postgress is columnDefinition = "serial"
2

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
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.