santus say
santus say

Reputation: 3

How to map enum with ebean?

I am using play framework 2 and ebean I have such enum, and saving Integer id at database

public enum Permission {
local$company$company_panel(2_001, "local.company.company_panel", "вход в компанийскую админку ")
Integer id;
String name;
String description;

Permission(Integer id, String name, String description) {
    this.id = id;
    this.name = name;
    this.description = description;
}

@DbEnumValue(storage = DbEnumType.INTEGER)
public Integer getId() {
    return id;
}

public String getName() {
    return name;
}

public String getDescription() {
    return description;
}

public static Permission findById(Integer id) {
    for(Permission permission : Permission.values()) {
        if(permission.getId().equals(id)) {
            return permission;
        }
    }
    return null;
}

}

Then I have this collection

@DbArray
@Column(name = "permissions")
private List<Permission> permissions = new ArrayList<>();

And when i try to get enum from database, i have such error: Caused by: java.lang.IllegalArgumentException: No enum constant models.permission.Permission.2001

Upvotes: 0

Views: 625

Answers (1)

Tom
Tom

Reputation: 1457

If this is a copy/paste of your code, you wrote "2_001" instead of "2001" in your enum definition.

Update :

Seeing how your enum is constructed, If you want to use local$company$company_panel, It would seem you have two way of doing that :

Permission myPermission = Permission.local$company$company_panel

or

Permission myPermission = Permission.getById(2001)

If you want to be able to access it using Permission.2001 you need to name it 2001 and not local$company$company_panel

Upvotes: 0

Related Questions