如何使字符串主键休眠。 @GeneratedValue策略 [英] How to make string primary key hibernate. @GeneratedValue strategies
问题描述
对不起,如果这个问题已被问到,我已阅读了一些文章,但没有找到答案。
我的目标是创建具有唯一字段IMEI的实体设备我想用它作为主键,并在设备注册时指定它(手动指定,同时创建实体)。
我使用Spring roo工具进行开发,并使用hibernate作为ORM。
当我在Entity声明中指定此项时:
@RooJavaBean
@RooToString
@RooJpaActiveRecord(identifierField =IMEI,identifierType = String.class)
public class Device {.. 。
我得到了这个生成的。
<$
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =IMEI)
private String Device.IMEI;
使用mysql db将项目部署到服务器时出现此错误
2013-03-17 20:03:23,136 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389:Unsuccessful:create table device(imei varchar 255)not null auto_increment,model varchar(255),name varchar(255)not null,版本整数,主键(imei))
2013-03-17 20:03:23,136 [main] ERROR org.hibernate .tool.hbm2ddl.SchemaExport - 列'imei'的列说明符错误
然后我覆盖roo生成的字段与
@Id
@GeneratedValue(generator =org.hibernate.id.Assigned)
@Column(name =the_code)
私人字符串代码;
(我在听到)
但仍然出错,然后我将代码简单地改为这个
@Id
私人字符串IMEI;
然后它工作正常并要求我在保存实体前指定imei字段。
我的问题是:
使用我的自定义字段作为id是正确的。
可以通过字符串
确定它不是用休眠生成的,而是从设备imei。
什么是org.hibernate.id.Assigned?b
$ b
为什么roo为@RooJpaActiveRecord生成代码(标识符字段=IMEI,标识符类型= String.class)不起作用。
可以创建字符串自动生成的主键
什么是默认的GeneratedValue策略值(我的最后一种情况)
我读过官方文档,但没有明白,请转到我的文章, code> @GeneratedValue(strategy = GenerationType.AUTO)不能与 String
类型一起使用。所以,如果你想使用 String
作为ID,你必须手动赋值。但如果String适合您的需要,则可以使用String。
当 @GeneratedValue
注释不是默认是分配生成器,这意味着标识符的值必须由应用程序设置。
请参阅 hibernate手册了解详情。
Sorry if this question was already asked, I've read some articles but didnt find the answer.
My goal is to create entity Device that has unique field IMEI and i would like to use it as primery key, and specify it at device registration time (manualy specified, while creating the entity). I use Spring roo tool for development and hibernate as ORM.
When I specify this in Entity declaration:
@RooJavaBean
@RooToString
@RooJpaActiveRecord(identifierField = "IMEI", identifierType = String.class)
public class Device {...}
i get this generated.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "IMEI")
private String Device.IMEI;
While deploying project to server with mysql db i get this error
2013-03-17 20:03:23,136 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table device (imei varchar(255) not null auto_increment, model varchar(255), name varchar(255) not null, version integer, primary key (imei))
2013-03-17 20:03:23,136 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Incorrect column specifier for column 'imei'
Then I override roo generated field with
@Id
@GeneratedValue(generator = "org.hibernate.id.Assigned")
@Column(name = "the_code")
private String code;
(I've found this in hear)
but still get error, then I've changed code simply to this
@Id
private String IMEI;
and then it works fine and asks me to specify imei field before saving entity.
My questions is:
is it correct to use my custom field as id.
is it ok to by String
is it ok that it is not generated with hibernate but taken from the device imei.
what is org.hibernate.id.Assigned
why roo generated code for @RooJpaActiveRecord(identifierField = "IMEI", identifierType = String.class) does not work.
is it posible to create String auto generated primery key
what is default GeneratedValue strategy value (my last case)
i've readofficial doc but didn't Understand all, please refer me to article where i cen read about all this.
Thanks, sorry for long question.
@GeneratedValue(strategy = GenerationType.AUTO)
cannot be used with String
type. So, if you want to use String
as ID, you have to assign it manually. But it is fine to use String as ID if it suits your need.
Using org.hibernate.id.Assigned
also means that you have to assign the ID value before saving the data.
When @GeneratedValue
annotation is not added, the default is assigned generator which means the value of identifier has to be set by the application.
Please refer to the hibernate manual for details.
这篇关于如何使字符串主键休眠。 @GeneratedValue策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!