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

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

问题描述

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

解决方案

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

,您将其保留为 null 0 )。 ( null 如果使用整数 / Long 包装器)

在某些情况下, AUTO 策略已解析为 SEQUENCE rathen比 IDENTITY TABLE ,因此您可能需要手动将其设置为 IDENTITY 或 TABLE (取决于底层数据库)。



SEQUENCE + 指定序列名称适用于您。


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;

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.

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

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