Grails:如何从多对多关系中删除记录?

时间:2016-09-05 11:31:54

标签: grails gorm grails-3.0

应用信息:

  • Grails版本:3.1.8
  • Groovy版本:2.4.6
  • JVM版本:1.8.0_60

我的域名类:

class AgePhase {

    String agePhase

    static hasMany = [assessors: Assessor]

其他域类:

class Assessor {

    static belongsTo = [AgePhase]

    static hasMany = [agePhase: AgePhase]

在我的数据库中,我有Assessor,AgePhase和join assessor / agePhase表。

如何根据agePhase ID和Assessor ID从assessor / agePhase表中删除记录?

2 个答案:

答案 0 :(得分:0)

你需要调用removeFrom对象上的AgePhase(因为它属于这个类)并给出一个Assessor对象作为参数。

示例:

def agePhase = AgePhase.get(agePhaseId)
def assessor = Assessor.get(assessorId)

agePhase.removeFromAssessors(assessor)

这将删除agePhaseId和assessorId

的连接表中的条目

答案 1 :(得分:-1)

如果要根据 agePhaseId assessorId 从关系表中删除记录,可以尝试以下操作:

def agePhase = AgePhase.get(agePhaseId)
def assesor = agePhase.assessors.find{ it.id == assessorId }
agePhase.removeFromAssessors(assesor)

Grails文档中的参考:removeFrom