grails - Gorm一对一和一对多

标签 grails gorm one-to-many one-to-one

我有两个对象具有一对一和一对多的关系。

class GroupMember {
   /**
    * InvitationCode used to join this Group
    */
   InvitationCode invitationCode

   static constraints = {
      // group creator member object won't have used an invitation code
      invitationCode(nullable: true) 
   }
}

class InvitationCode {
   /**
    * GroupMember who created the code, not used the code
    */
   GroupMember creator

   // I tried removing this, it has no effect.
   static hasMany = [members: GroupMember]
}

基本上,我有一个GroupMember,他拥有一个InvitationCode,并且可以使用另一个InvitationCode。或者,一个InvitationCode只能属于/由一个GroupMember创建,但可以被许多GroupMember使用/引用。

这些表看起来好像已正确设置-两个表都有另一个字段:INVITATION_CODE.CREATOR_IDGROUP_MEMBER.INVITATION_CODE_ID

但是,当我创建一个新的InvitationCode时,似乎GroupMember对象正在更新,并且invitationCodeId被设置为新创建的InvitationCodeid
GroupMember creator = new GroupMember(
   invitationCode: null // group creator - didn't use an invitation code
)
creator.save(flush: true)

InvitationCode invitationCode = new InvitationCode(
   creator: creator
)
invitationCode.save(flush: true)

GroupMember _creatorMember = GroupMember.findById(creator.id)
assert _creatorMember.invitationCode == invitationCode // true??

我从来没有设置creator.invitationCode,所以我不确定GORM如何/为什么设置它。我也不确定我在定义域时的错误。当我删除flush: true时,我对GroupMember.invitationCode违反了外键约束。

最佳答案

当前设置的问题是InvitationCode域对GroupMember域有2个引用,而grails错误地(在您的情况下)推断出:

  • invitationCodecreator彼此双向关联
  • members是与GroupMember
  • 的单向关联

    但实际上,您想要:
  • creator是与GroupMember
  • 的单向关联
  • members与反向引用invitationCode成多对一关系,使其成为双向关联

  • 可以理解,grails在“猜测”如何形成这种复杂的关系方面存在问题。

    关键将是 mappedBy 属性的使用。我已经成功地将其用于在相同的2个类上映射2个多对多关系,但没有一个像您需要的那样。我认为您需要的是在InvitationCode类中包含以下内容:
    static mappedBy = [
        members: 'invitationCode',
        creator: 'none' // because the relationship is unidirectional
    ]
    

    希望这对您有用!

    关于grails - Gorm一对一和一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957496/

    相关文章:

    grails - 默认情况下,标准使用 “inner join”而不是 “left join”方法使我的查询不按我计划的方式工作

    grails - Grails:每当删除主域对象时,如何删除关联的域?

    grails - 如何实现Grails域类的投票?

    c# - Entity Framework 5 Code First 中的一对一和一对多关系

    java - 使用 JPA 从集合中删除子项

    validation - Doctrine 2.0 一对多模式验证问题

    grails - HQL加入Grails:部分Deux

    Grails 从计划的作业中调用 Controller 方法

    grails - 如何在 groovy 域中创建引用关系?

    grails - 无法初始化代理 - 无 session ,正在访问过滤器类中的 session 对象