简体   繁体   中英

Grails mongodb-morphia plugin - dynamically set domain collection mapping

I would like to map a domain class to a DBCollection based on a param set in a grails app. I'm relatively new to Spring, Grails, and MongoDB.

I have a mongodb database with several large collections, and I would like to let the user select which collection to map the domain class to. Ideally, the domain class could be remapped over and over again, switching between different collections.

I'm designing the front end of the web-app, with the back-end being handled elsewhere...I'll know the Mongodb db name, but the number and name of the collections within that db will be unknown.

Unfortunately, Morphia's Entity notation only accepts a String constant, so I can't pass something dynamic to that. I'm thinking that I need to manually unmap the domain class, change the collection settings, and remap the class (delete beans maybe?). I'd appreciate any suggestions on how to accomplish this, preferably using the mongodb-morphia plugin.

I created a mongodb domain class similar to this:

package x

import java.util.Date;
import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;

@Entity("someCollection")
class MongoLog {

        def Mongo
        def someService

        Date a
        String b
        String c
        Double d

        static constraints = {
                a(nullable:true)
                b(nullable:true)
                c(nullable:true)
                d(nullable:true)
        }
}

PS I tried using static mapping, but the mongodb-morphia plugin didn't seem to register the collection I specified.

Thanks

You should read the User-Guide provided with the Morphia-Plugin. See here: http://jkuehn.github.com/gorm-mongodb/

You're thinking too hibernaty ;)

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