2

is there a special way with gorm to map a three domain classes relationship like this:

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

Thanks in advance.

4

2 回答 2

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。但似乎我必须做很多保存()。不要忘记制作控制器并设置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 = {
    }
} 
于 2010-02-16T00:30:22.337 回答
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)
  }
}

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

于 2010-02-16T17:31:23.650 回答