How to make a sleeping primary key of a sleeping mode. @GeneratedValue Strategy

My goal is to create an entity device with a unique IMEI field, and I would like to use it as a primary key and specify it during device registration (specified manually when creating an entity). I am using Spring roo tool for development and sleeping as an ORM.

When I state this in an 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; 

When deploying a project to a 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 redefine the generated field with

 @Id @GeneratedValue(generator = "org.hibernate.id.Assigned") @Column(name = "the_code") private String code; 

(I found it here )

But still getting an error. Then I changed the code just like this:

  @Id private String IMEI; 

and then it works fine and asks me to specify the imei field before saving the object.

My questions:

Is it correct to use my custom field as an identifier?

Is this normal for String?

is it normal that it is not generated with hibernate but taken from an imei device?

What is org.hibernate.id.Assigned?

Why doesn't the generated roo code for @RooJpaActiveRecord (identifierField = "IMEI", identifierType = String.class) not work?

Is it possible to create a String automatically generated primary key?

What is the default GeneratedValue strategy value (my last case)?

I read the official document, but did not understand everything, please send me to an article where I can read about all this.

Thank you, sorry for the long question.

+6
source share
3 answers

@GeneratedValue(strategy = GenerationType.AUTO) cannot be used with type String . So, if you want to use String as an identifier, you need to assign it manually. But it's ok to use String as an identifier if it suits you.

Using org.hibernate.id.Assigned also means that you must assign an ID value before saving the data.

When the @GeneratedValue annotation @GeneratedValue not added, a generator is assigned by default, which means that the identifier value must be set by the application.

See the hibernation guide for more information.

+11
source

A simple solution might be to use the @PrePersist annotation for your entity class.

Just add a method

 @PrePersist private void ensureId(){ this.setId(UUID.randomUUID().toString()); } 

and get rid of the @GeneratedValue annotation.

PrePersist Documentation: http://docs.oracle.com/javaee/5/api/javax/persistence/PrePersist.html

Stefano

+15
source

This may not be necessary at the moment. But I think we should renew this ticket for someone.

I'm new to stack overflow answer, so hopefully this makes sense

If you want to automatically generate a String as an ID in hibernate, you can define a rule using IdentifierGenerator and @GenericGenerator.

Object declaration:

 public class Device {... @Id @GenericGenerator(name = "sequence_imei_id", strategy = "com.supportmycode.model.ImeiIdGenerator") @GeneratedValue(generator = "sequence_imei_id") @Column(name = "IMEI") private String IMEI; ...} 

Have a Generator Declaration:

 public class ImeiIdGenerator implements IdentifierGenerator {... public Serializable generate(SessionImplementor session, Object object) throws HibernateException { // define your IMEI, example IMEI1, IMEI2,...; return "IMEI"+ UUID.randomUUID().toString(); ...} 

When you save a Device object, ImeiIdGenerator automatically generates IMEI (id).

+2
source

All Articles