【问题标题】:Grails many to many relationship errorGrails多对多关系错误
【发布时间】:2013-11-05 12:55:26
【问题描述】:

由于我对 GORM 和 Grails 中的域对象建模知识贫乏,我遇到了一个问题。

这是我的问题:

| Error Error loading plugin manager: 
No owner defined between domain classes [class com.myproject.model.Project] and 
[class com.crowdfun.Sector] in a many-to-many relationship. 
Example: static belongsTo = com.myproject.model.Sector 
(Use --stacktrace to see the full trace)

我不能说哪里出了问题,因为我按照官方grails文档的教程:http://grails.org/doc/latest/guide/GORM.html#manyToMany

我的课:

Project.groovy:

class Project {

    String name
    Integer nbInvestors
    Region region
    Integer nbDays
    Boolean success
    String equity
    String currency
    Double target
    Double raisedAmount
    String url
    Double valuation

    boolean extended = false

    static belongsTo = [
        site: Site,
        sector: Sector
    ]

    static hasMany = [
        sectors: Sector
    ]

    static hasOne = [
        valuationRange: ValuationRange,
        targetRange: TargetRange
    ]

    static constraints = {
        name nullable: true
        nbInvestors nullable: true
        region nullable: true
        nbDays nullable: true
        success nullable: true
        equity nullable: true
        currency nullable: true
        target nullable: true
        raisedAmount nullable: true
        url nullable: true, unique: true
        valuation nullable: true
    }
}

Sector.groovy:

class Sector {

    String name

    static hasMany = [
        projects: Project
    ]

    static constraints = {
        name unique: true
    }

    @Override
    public String toString() {
        return name
    }

    def getNbProjects() {
        projects.size()
    }
}

Site.groovy

class Site {

    String name

    static hasMany = [
        projects: Project
    ]

    static constraints = {
        name unique: true
    }

    @Override
    public String toString() {
        return name
    }
}

【问题讨论】:

  • 错误是不言自明的。使用 belongsTo = Sector 表示 m-m 关系。
  • 我不能这样做,因为我曾经在我的项目类中添加过 belongsTo 关系。所以我这样做了: static belongsTo = [ site: Site, sector: Sector ] 似乎是正确的不?
  • 再次逐行、逐字、逐句地按照相同的教程进行操作。 :)

标签: grails grails-orm


【解决方案1】:

像这样改变类:

class Project {

    ...
    Site site
    Sector sector

    static belongsTo = [Site, Sector]
}

【讨论】:

  • 谢谢,这就是重点,你有解释为什么这样做是正确的吗?因为我不明白之间的区别:静态 belongsTo = [sector: Sector, site: Site] 和你的解决方案 =)
  • 在 belongsTo 中只使用类名来关闭反向引用。
猜你喜欢
  • 1970-01-01
  • 2016-07-08
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多