How to make string primary key hibernate. @GeneratedValue strategies

ε祈祈猫儿з 提交于 2020-01-19 12:55:29

问题


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.


回答1:


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




回答2:


A straightforward solution could be to use the @PrePersist annotation on your entity class.

Simply add the 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




回答3:


At the moment, it may be unnecessary. But I think we should update this ticket for someone.

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

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

Entity 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;

...}

Imei 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, IMEI(id) will be generate automatically by ImeiIdGenerator.



来源:https://stackoverflow.com/questions/15466518/how-to-make-string-primary-key-hibernate-generatedvalue-strategies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!