【问题标题】:Groovy Grails Many to Many association creating 2 join tablesGroovy Grails 多对多关联创建 2 个连接表
【发布时间】:2011-10-26 14:51:45
【问题描述】:

所以我有 2 个具有多对多关联的类。生成数据库后,我得到 2 个连接表:user_link_sections 和 link_section_users。我期望的第一个,但不确定为什么会生成第二个连接表。 Grails 也只使用第一个。我是 groovy/grails 的菜鸟,但尝试按照文档进行正确设置。任何帮助表示赞赏!

这是我的域类声明...

class User {

    transient springSecurityService

    // Custom Attributes
    String firstName
    String lastName
    String email
    String company
    String phone
    Date dateCreated
    Date lastUpdated

    static hasMany = [linkSections: LinkSection]

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
}

class LinkSection {

    String name
    String description
    Date dateCreated
    Date lastUpdated

    List links = new ArrayList()

    static belongsTo = User
    static hasMany = [links: Link, users: User]

    static constraints = {
    name blank: false
    description(maxSize: 10000)
    }

    static mapping = {
    links cascade: "all,delete-orphan"
    }
}

【问题讨论】:

    标签: grails groovy grails-orm


    【解决方案1】:

    尝试设置多对多的拥有方:

    static belongsTo = User
    

    这在 LinkSection 上进行。请记住,只有一方知道维持这种关系。

    要做的另一件事是确保并清除您之前尝试映射时可能留下的表格......只是为了确定。

    【讨论】:

    • 感谢 Dmitry 的回复。我在上面的代码中确实有 belongsTo ......我目前允许它在启动时删除/重新创建表(我仍然处于重型开发模式)。还有其他想法吗?
    • 我知道我现在相信的是什么。这是你的第二点——我认为使用 drop/recreate 设置它会删除所有表,但它显然只删除它认为应该存在的表。我清除了额外的表,它没有在运行时重新生成它。问题解决了!
    猜你喜欢
    • 2019-08-22
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多