开发者

grails - how to save ArrayList in db

开发者 https://www.devze.com 2023-03-13 15:04 出处:网络
I use grails-1.3.2 and hbase plugin. I have some difficulty in creating one-to-Many association with hbase (i can work with hibernate), so

I use grails-1.3.2 and hbase plugin. I have some difficulty in creating one-to-Many association with hbase (i can work with hibernate), so i decided to try create one-to-Many association with using ArrayList.

Here are my domain classes and co开发者_高级运维ntrollers:

class Contacts {

    String con   

    static constraints = {}
}

class ContactsController {

    def create = {
        def contact = new Contacts()
        contact.con = params.con
        contact.save(flush:true)
    }
}

class User {

    String firstname
    String lastname

    // static hasMany = [contact: Contacts]

    static ArrayList<Contacts> contact    

    static constraints = {}
}

class UserController{

    def create = {
        def user = new User()

        user.properties = params
        user.save(flush: true)
    }

    def addContact = {
        def user = User.get(params.userID)
        def contact = Contacts.get(params.contactID)

        user.contact.add(contact)
        user.save(flush:true)
    }
}

In addContact action user.contact = null, so it can not work. In user does nor appear contact field.

Can someone help me understand what i have to do for saving ArrayList in db?


I don't know anything about hbase, but the static contact property of the User class looks very suspicious. The fact that this property is static, implies that every user has the same contact list, which seems unlikely to be the desired behaviour.

In a standard GORM domain model - assuming you want each User to have their own contact list - this would be defined

class User {    
    String firstname
    String lastname

    static hasMany = [contact: Contacts]
}

Although it looks like we're also defining a static property here, it's actually just the definition of how the Contact and User classes are related (AKA mapping) that is static. The contact property that is dynamically added to the User class is non-static.

Aside

I recommend renaming the Contacts class to Contact and the contact property to contacts. The GORM mapping would then look like this:

class User {    
    String firstname
    String lastname

    static hasMany = [contacts: Contact]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消