Persisting string array in single column
Our main domain object has multiple string[] properties (various configuration options) and we are thinking of an elegant way to persist the data. GORM creates joined table for th开发者_如何学Goe each array so we end up with about dozen joined tables.
I wonder if it would be possible to serialize each array into single column of the main table (someway delimited) and parse it back into array onload?
Do you have suggestions how to do this? I'm thinking either hibernate usertype or grails property editor? I spent some time with usertypes but without luck.
thanks pk
you could put the parameters into a map / array, then store them in a db field as Json:
def someDomainInstance = new SomeDomain()
def paramMap = [name:'John', age:24]
someDomainInstance.paramJson = paramMap as JSON
someDomainInstance.save()
Then you can easily convert this string back to a map / array when you interrogate the DB:
def paramMapFromDB = JSON.parse(someDomainInstance.paramJson)
assertEquals 24, paramMapFromDB.age
Something like that, I haven't tested the syntax, but that's the general idea.
精彩评论