休眠自动增量 ID [英] Hibernate Auto Increment ID

查看:40
本文介绍了休眠自动增量 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用带有注释的休眠的 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?

推荐答案

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

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

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

在某些情况下,AUTO 策略被解析为 SEQUENCE 而不是 IDENTITYTABLE,所以你可能需要手动将其设置为 IDENTITYTABLE(取决于底层数据库).

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

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

这篇关于休眠自动增量 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆