Java 休眠自动增量 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2011528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:29:45  来源:igfitidea点击:

Hibernate Auto Increment ID

javahibernateauto-increment

提问by cedric

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?

我有一个使用带有注释的休眠的 j2ee 应用程序。如何在我的 pojo 类中注释 Id 字段以将其设置为自动增量或自动生成。在添加 bean 时,我是否将该字段留在我的 bean 中为空?

采纳答案by Bozho

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

and you leave it null(0) when persisting. (nullif you use the Integer/ Longwrappers)

而你在坚持时离开它null0)。(null如果您使用Integer/Long包装器)

In some cases the AUTOstrategy is resolved to SEQUENCErathen than to IDENTITYor TABLE, so you might want to manually set it to IDENTITYor TABLE(depending on the underlying database).

在某些情况下,AUTO策略被解析为SEQUENCErathen而不是IDENTITYTABLE,因此您可能需要手动将其设置为IDENTITYTABLE(取决于底层数据库)。

It seems SEQUENCE+ specifying the sequence nameworked for you.

似乎SEQUENCE+指定序列名称对您有用。

回答by alfonx

If you have a numeric column that you want to auto-increment, it might be an option to set columnDefinitiondirectly. 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:

如果您有一个想要自动递增的数字列,它可能是一个columnDefinition直接设置的选项。这样做的好处是,即使在没有休眠的情况下使用架构也会自动生成值。这可能会使您的代码特定于数据库:

import javax.persistence.Column;
@Column(columnDefinition = "serial") // postgresql

回答by Kaushik Lele

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

您可以使用任意名称代替 kaugen。它运行良好,我可以在控制台上看到以下查询

Hibernate: select max(proj_id) from javaproj
Hibernate: insert into javaproj (AUTH_email, AUTH_firstName, AUTH_lastName, projname,         proj_id) values (?, ?, ?, ?, ?)

回答by jmoreira

FYI

供参考

Using netbeansNew Entity Classes from Databasewith a mysql*auto_increment* column, creates you an attribute with the following annotations:

使用带有mysql*auto_increment* 列的数据库中的netbeans新实体类,为您创建一个具有以下注释的属性:

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

这让我得到了同样的错误,指出列不能为空,所以我只是删除了@NotNull 注释,使属性为空,并且它有效!

回答by user2530633

Hibernate defines five types of identifier generation strategies:

Hibernate 定义了五种类型的标识符生成策略:

AUTO- either identity column, sequence or table depending on the underlying DB

AUTO- 标识列、序列或表取决于底层数据库

TABLE- table holding the id

TABLE- 保存 id 的表

IDENTITY- identity column

IDENTITY- 身份列

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

有关更多详细信息,请查看链接

回答by Henrique C.

In case anyone "bumps" in this SO question in search for strategies for Informixtable when PKis type Serial.

PK类型为Serial时,如果有人在这个 SO 问题中“碰撞”以搜索Informix表的策略。

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.SaveOrUpdateyou pass the value for the column special_serial_pkNULL.

为此,请确保在执行session.SaveOrUpdate时传递special_serial_pk NULL列的

In my case i do an HTMLPOSTwith JSONlike so...

在我的情况下,我用JSON做一个HTML POST像这样......

{
"special_serial_pk": null, //<-- Field to be incremented
"specialcolumn1": 1,
"specialcolumn2": "I love to code",
"specialcolumn3": true
}

回答by Ankur Patel

Using netbeans New Entity Classes from Database with a mysql auto_incrementcolumn, creates you an attribute with the following hibernate.hbm.xml: id is auto increment

将数据库中的 netbeans 新实体类与 mysql auto_increment列一起使用,为您创建一个具有以下 hibernate.hbm.xml 的属性:id 是自动增量