Hibernate自动递增ID

发布于 2024-08-17 03:02:02 字数 105 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷弦 2024-08-24 03:02:03

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

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

TABLE - 保存 id 的表

IDENTITY > - 身份列

SEQUENCE - 序列

身份复制 – 身份是从另一个实体复制的

示例使用表

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

了解更多详细信息,请检查链接

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.

我不吻晚风 2024-08-24 03:02:03

如果您有一个想要自动递增的数字列,则可以选择直接设置columnDefinition。这样做的优点是,即使在没有休眠的情况下使用该模式,模式也会自动生成该值。但这可能会使您的代码特定于数据库:

import javax.persistence.Column;
@Column(columnDefinition = "serial") // PostgreSQL
@Column(columnDefinition = "integer auto_increment") // MySql

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
夏了南城 2024-08-24 03:02:03

PK 类型为 Serial 时,如果有人在搜索 Informix 表的策略时在此 SO 问题中“遇到”问题。

我发现这有效......作为一个例子。

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

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

就我而言,我使用 JSON 执行 HTML POST ,如下所示......

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

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
}
无法言说的痛 2024-08-24 03:02:03

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

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

恋你朝朝暮暮 2024-08-24 03:02:02
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

并且在持久化时将其保留为 null (0)。 (null 如果您使用 Integer / Long 包装器)

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

看来SEQUENCE + 指定序列名称对你有用。

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

×纯※雪 2024-08-24 03:02:02

按如下方式操作:-

@Id
@GenericGenerator(name="kaugen" , strategy="increment")
@GeneratedValue(generator="kaugen")
@Column(name="proj_id")
  public Integer getId() {
    return id;
 }

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

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

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 (?, ?, ?, ?, ?)
薆情海 2024-08-24 03:02:02

仅供参考,

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

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

这让我很困惑同样的错误说该列不能为空,所以我只是删除了 @NotNull 注释,留下属性为空,并且它有效!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文