简体   繁体   中英

Redis (grails plugin) does not persist Enum Object

I've got a grails domain class I have to persist in Redis, something like this:

class A {
    String one
    Integer two

    B three

    E four

    mapWith = "redis"
}

class B {
    String name
}

enum E {
   VALUE1, VALUE2
}

When I persist an instance of class A with the GORM .save() method, Redis saves it correctly except for the enum field "four".

As you can see the fact is known and reported here: http://jira.grails.org/browse/GPREDIS-3

Is there a good workaround to save Enum or something similar? We're thinking about an array of String objects, what do you think?

I've got this mostly implemented but it doesn't work for Gemfire and I'm waiting until it's fixed for all the supported nosql providers before pushing the fix. As a workaround you can use the inList constraint with a combination of a persistent String property and a non-persistent get/set pair with the name of your current property, eg

class A {
   String one
   Integer two

   B three

   String fourString

   void setFour(E e) {
      fourString = e?.name()
   }
   E getFour() {
      fourString ? E.valueOf(fourString) : null
   }

   static constraints = {
      fourString inList: E.values()*.name()
   }

   static transients = ['fourString']

   static mapWith = "redis"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM