GORM中的三个域类关系

时间:2010-02-15 22:18:05

标签: grails gorm

gorm是否有一种特殊的方式来映射这样的三个域类关系:

1 person belongs to N companies with M given roles (one or more roles for a given company)

提前致谢。

2 个答案:

答案 0 :(得分:2)

看看http://www.grails.org/Many-to-Many+Mapping+without+Hibernate+XML(我认为它是最新的)。

请注意:http://codedumpblog.blogspot.com/2010/02/grails-many-to-many-with-lists.html

下面的代码对我来说使用grails 1.2.0。但似乎我必须做很多save()的事情。不要忘记制作控制器并设置scaffold = domain_class

package p

class Company {
    String toString() { "$name"
    }
    static hasMany=[roles:Role]
    static constraints = {
    }
    String name
}

package p

class Role {
    String toString() { "$name"
    }
    static belongsTo=[company:Company]
    static hasMany=[personRoleAssociations:PersonRoleAssociation]
    static constraints = {
    }
    String name
}

package p

class Person {
    String toString() { "$name"
    }
    static hasMany=[personRoleAssociations:PersonRoleAssociation]
    static constraints = {
    }
    String name
}

package p

class PersonRoleAssociation {
    String toString() { "${person.name} as ${role.name}"
    }
        static belongsTo=[person:Person,role:Role]
    static constraints = {
    }
}


import p.*
class BootStrap {

    def init = { servletContext ->
        Person dick=new Person(name:'dick')
        Person jane=new Person(name:'jane')
        dick.save()
        jane.save()
        Company ibm=new Company(name:'ibm')
        ibm.save()
        Role ibmManager=new Role(name:'ibmmanager')
        Role ibmPeon=new Role(name:'ibmpeon')
        ibm.addToRoles(ibmManager)
        ibmManager.save()
        ibm.addToRoles(ibmPeon)
        ibmPeon.save()
        ibm.save()
        Company sun=new Company(name:'sun')
        sun.save()
        Role sunManager=new Role(name:'sunmanager')
        Role sunPeon=new Role(name:'sunpeon')
        sun.addToRoles(sunManager)
        sunManager.save()
        sun.addToRoles(sunPeon)
        sunPeon.save()
        sun.save()
        PersonRoleAssociation dickManager=new PersonRoleAssociation()
        dick.addToPersonRoleAssociations(dickManager)
        ibmManager.addToPersonRoleAssociations(dickManager)
        PersonRoleAssociation dickPeon=new PersonRoleAssociation()
        dick.addToPersonRoleAssociations(dickPeon)
        sunPeon.addToPersonRoleAssociations(dickPeon)
        PersonRoleAssociation janeManager=new PersonRoleAssociation()
        jane.addToPersonRoleAssociations(janeManager)
        sunManager.addToPersonRoleAssociations(janeManager)
        PersonRoleAssociation janePeon=new PersonRoleAssociation()
        jane.addToPersonRoleAssociations(janePeon)
        ibmPeon.addToPersonRoleAssociations(janePeon)
    }
    def destroy = {
    }
} 

答案 1 :(得分:1)

我会尝试:

class Person {
  String name
  Set<Role> roles
  Set<Company> companies

  public String toString() {
    return name + " roles : " + (roles.collect { it.name }).toString() + " - companies : " + (companies.collect { it.name }).toString()
  }

  static hasMany = [companies:Company, roles:Role]
  static constraints = {
    name (unique:true)
    roles (nullable:true)
  }
}


class Role {
  String name
  String toString() {
    return name + " companies : " + (companies.collect { it.name }).toString() 
  }
  static hasMany = [companies : Company]
  static belongsTo = Company
  static constraints = {
    name (unique:true)
    companies (nullable:false)
  }
}

class Company {
  String name
  String toString() {
    return name + " roles : " + (roles.collect { it.name }).toString()
  }
  static hasMany = [roles : Role]
  static constraints = {
    name (unique:true)
    roles (nullable:true)
  }
}

虽然没有测试......我有兴趣知道我的解决方案是否有任何问题以及它们可能是什么......