Nnamdi Jibunoh
Nnamdi Jibunoh

Reputation: 156

Generating UUID based @ID for Playframework 2.3x

Good day Guys,

I want to create Models that don't use the default @Id auto generation for Playframework and Ebeans. I have seen online that there are options for Using GenericModel, however that class doesn't seem to be included in version 2.3x. I have done this in order to workaround it but i still fall short my aim

public class ProductVariants extends Model  
{  
    @Id
    String id; 
    public String getId() {
        return (this.id == null) ? UUID.randomUUID().toString()  : this.id;
    }

    public void setId(String id) {
        this.id = id;
    } 
}

The issue with this is that i have to manually set the ID before i can save the object e.g.

productVariant.setId(productVariant.getId());
productVariant.save();

Both for a primary model and all it related models with a OneToMany relationship, and it is currently giving me issues when i bind from the view to the Model object with error ERROR executing DML bindLog[] error[Field 'id' doesn't have a default value]]].

Please any help will be appreciated.

Upvotes: 0

Views: 851

Answers (1)

Nnamdi Jibunoh
Nnamdi Jibunoh

Reputation: 156

Good day Guys,

I finally fixed this by using the UUID class that ships with the JDK. So when you are creating your Models you create them with the

@Id  
public java.util.UUID id

Also in the routes file if you need to map to a record by the ID you can do that by doing something like this
GET /:pid/edit controllers.Application.edit(pid: java.util.UUID)

Upvotes: 1

Related Questions