Hibernate Auto Increment Identifier

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

+74
java hibernate auto-increment
Jan 6 '10 at 7:40
source share
7 answers
@Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; 

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

In some cases, the AUTO strategy is allowed before SEQUENCE rathen than before IDENTITY or TABLE , so you can manually set it to IDENTITY or TABLE (depending on the underlying database).

SEQUENCE + seems to be giving the sequence name for you.

+141
Jan 06 '10 at 7:45
source share

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 requests on the console

 Hibernate: select max(proj_id) from javaproj Hibernate: insert into javaproj (AUTH_email, AUTH_firstName, AUTH_lastName, projname, proj_id) values (?, ?, ?, ?, ?) 
+31
Apr 19 2018-12-12T00:
source share

Hibernate defines five types of identifier generation strategies:

AUTO - either an identifier column, sequence or table depending on the underlying database

TABLE - table with identifier

IDENTIFICATION - identifiers column

SEQUENCE - sequence

copy of identity - the identity is copied from another object

Table example

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

+7
Aug 25 '14 at 17:16
source share

Fyi

Using netbeans New Entity classes from a database with a mysql * auto_increment * column, create you an attribute with the following annotations:

 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id") @NotNull private Integer id; 

This led me to the same error that the column should not be null, so I just removed @NotNull anonymity, leaving the null attribute on and it works!

+6
Jul 27 '12 at 15:58
source share

If you have a numeric column that you want to automatically grow, it may be possible to set columnDefinition directly. This has the advantage that the circuit automatically generates a value, even if it is used without sleep mode. This may make your code db-specific, though:

 import javax.persistence.Column; @Column(columnDefinition = "serial") // postgresql 
+4
Feb 04 '11 at 8:52
source share

In case someone β€œencounters” this SO question in finding strategies for an Informix table when PK is of type Serial .

I found this to work ... as an example.

 @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "special_serial_pk") private Integer special_serial_pk; 

For this to work, make sure that when you execute session.SaveOrUpdate, you pass the value for the special_serial_pk NULL column.

In my case, I am doing POST HTML with JSON, like ...

 { "special_serial_pk": null, //<-- Field to be incremented "specialcolumn1": 1, "specialcolumn2": "I love to code", "specialcolumn3": true } 
0
Mar. 27 '19 at 9:38
source share

Using netbeans, the new entity classes from the database with the mysql auto_increment column create an attribute with the following hibernate.hbm.xml: id is an auto increment

0
May 03, '19 at 6:00
source share



All Articles