Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make string primary key hibernate. @GeneratedValue strategies

My goal is to create an entity Device that has a unique field IMEI and I would like to use it as a primary key, and specify it at device registration time (manually 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 here)

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 are:

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 primary 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 can read about all this.

Thanks, sorry for long question.

like image 852
yaroslav prokipchyn Avatar asked Mar 17 '13 21:03

yaroslav prokipchyn


1 Answers

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

like image 142
Moon Avatar answered Jan 11 '23 23:01

Moon