A persistent array of strings in one column

Our main domain object has several string [] properties (various configuration options), and we're thinking of an elegant way to store the data. GORM creates a concatenated table for each array, so we end up with about a dozen concatenated tables.

I wonder if it is possible to serialize each array into one column of the main table (somehow delimited) and parse it back into an onload array?

Do you have any suggestions on how to do this? I am thinking either a hibernate custom type or a Grails property editor? I've spent some time with usertypes, but with no luck.

thanks pk

+2


a source to share


1 answer


you can put parameters in a map / array and then store them in the 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 polling the DB:



def paramMapFromDB = JSON.parse(someDomainInstance.paramJson)
assertEquals 24, paramMapFromDB.age

      

Something like this, I haven't tested the syntax, but that's the general idea.

+1


a source







All Articles