How to translate Enum to GreenDAO

I just started using greenDAO. How to add property Enum?

What I was thinking about: using the addIndex property of the object.

private static void main() { // TODO Auto-generated method stub static Schema blah; Entity unicorn = blah.addEntity("Weather"); unicorn.addIdProperty(); unicorn.addIntProperty("currentAirTemp"); unicorn.addIndex("shirtSize"); } 

Is this right to do?

Purpose: I want to have a link to shirtSize from the set: {XS, S, M, L, XL, XXL}

+6
source share
3 answers

As far as I know, Enums are not supported by greenDAO due to their instability. In addition, they are error-prone components for adding to the database logic, since the values ​​of enumeration elements can change.

One way around this would be to add the Int property to the database and then map the Enum ordinal values ​​in this field, for example:

 // add the int property to the entity unicorn.addIntProperty("shirtSize"); // create the enum with static values public enum ShirtSize { XS(1), S(2), M(3), L(4), XL(5), XXL(6); private final int value; private ShirtSize(int value) { this.value = value; } public int value() { return value; } } // set the ordinal value of the enum weather.setShirtSize(ShirtSize.XL.value()); 
+2
source

Using GreenDAO 3, we can now use the @convert annotation with the PropertyConverter

 @Entity public class User { @Id private Long id; @Convert(converter = RoleConverter.class, columnType = String.class) private Role role; enum Role { DEFAULT, AUTHOR, ADMIN } static class RoleConverter implements PropertyConverter<Role, String> { @Override public Role convertToEntityProperty(String databaseValue) { return Role.valueOf(databaseValue); } @Override public String convertToDatabaseValue(Role entityProperty) { return entityProperty.name(); } } } 

Read more at http://greenrobot.org/objectbox/documentation/custom-types/

+14
source

The latest version of GreenDao ( 2.x ) contains functionality that is ideal for your needs. Custom types exist that can easily serve enumerations.

Enum

 public enum ShirtSize { XS(1), S(2), M(3), L(4), XL(5), XXL(6); private final int value; ShirtSize(int value) { this.value = value; } public int value() { return value; } } 

Converter

 public class ShirtSizeConverter implements PropertyConverter<ShirtSize, Integer> { @Override public ShirtSize convertToEntityProperty(Integer databaseValue) { if(databaseValue == null) { return null; } else { for(ShirtSize value : ShirtSize.values()) { if(value.value() == databaseValue) { return value; } } throw new DaoException("Can't convert ShirtSize from database value: " + databaseValue.toString()); } } @Override public Integer convertToDatabaseValue(ShirtSize entityProperty) { if(entityProperty == null) { return null; } else { return entityProperty.value(); } } 

}

Declaring an entity field (in the generator)

 entity.addIntProperty("ShirtSize").customType( "com.your_package.ShirtSize", "com.your_package.ShirtSizeConverter" ); 
+7
source

Source: https://habr.com/ru/post/950684/


All Articles