问题
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?
回答1:
@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.
回答2:
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 (?, ?, ?, ?, ?)
回答3:
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!
回答4:
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.
回答5:
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
回答6:
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
}
回答7:
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
来源:https://stackoverflow.com/questions/2011528/hibernate-auto-increment-id