Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID with the Play Framework

I'd like to use UUID instead of the regular id on my models.

Can this be done with the play framework?

like image 478
fulmicoton Avatar asked Sep 13 '11 14:09

fulmicoton


2 Answers

First don`t extend (play.db.jpa.Model) Model in the Model you want to generate the Id but use GenericModel.

then you could use helper class that called when object is created (in constructor).

or call the helper class when saved(thus i have to create wrapper DAO, the save process is done in wrapper DAO not in the Object so that i could generate id the save the object)

or if you want more simple approach use JPA UUID. See code below.

@Entity
public class User extends GenericModel {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String id;
}
like image 195
indrap Avatar answered Jan 03 '23 12:01

indrap


Well, the Model class is just a sub-class of GenericModel which adds the attribute, methods and annotations to provide a generated Long as the @Id property for your model classes.

If you don't want that, you can subclass GenericModel instead and provide your own @Id. In your case that would be a String to hold the UUID. You'll need to come up with a strategy for initialising it on new model instances, though.

I'm not aware of a built-in strategy provided by JPA to generate UUIDs. A simple method would be to have a helper class that you can call a method on to get a new UUID and make sure you call that every time you create a new model.

like image 22
DougC Avatar answered Jan 03 '23 10:01

DougC