how to serialize domain classes grails

I tried to serialize grails domains classes to Maps or similar in order to be able to store it in memcached.

I want to be able to read the objects only, I don't need gorm crud. Only to read them without breaking the kind of interfaces they have.

For instance: I could convert domains to maps, becouse it wouldn't break the interface for access like .<property> or .findall or similar

First I tried to do a manual serialization but it was very error prone. So I needed something more general.

Then I tried to serialize as a map with a grails codec.

Here is the testing repo.

Here is the snippet.

But I get StackOverFlowException.

I also tried to mark all the domains as Serializable but I need to reattach every domain when I bring them back from memcached to avoid hibernate errors like org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Do you know a way to achieve this?

Is very frustrating to google search for something like this "storing domain classes in memcached" and find out is not a common problem.

Answers


I haven't see an out-of-the-box solution for doing this, but if you wanted to keep it generic you could do it manually (and consistently) like this:

def yourDomainInst = DefaultGrailsDomainClass(YourDomainClazz)
List listOfOnlyPersistantProperties = yourDomainInst.persistantProperties

def yourNewMap

yourObjects.each { obj ->
   listOfOnlyPersistantProperties.each { prop ->
      def propName = prop.name
      yourNewMap.put(propName, obj."$propName")
   }
}

Something like that should work. Note there's probably a hundred errors because I can't try it out right now, but that is the general idea.


Have a look at: Retrieving a list of GORM persistent properties for a domain


Need Your Help

Perl script that extracts data from HTML table

html perl table html-parsing

I need a Perl script to parse a simple HTML table with 2 columns.

Programatically firing an event in GTK (With Gtk2Hs)

haskell events gtk gtk2hs

I am using Gtk2Hs (EventM module) to handle GTK events within Haskell. Is it possible to manually (re-)fire an event?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.