Grails:如何从多对多关系中删除记录?
应用程序信息:Grails:如何从多对多关系中删除记录?,grails,gorm,grails-3.0,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] 在我的数
- 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表中删除记录?如果要基于agePhaseId和assessorId从关系表中删除记录,可以尝试以下操作:
def agePhase = AgePhase.get(agePhaseId)
def assesor = agePhase.assessors.find{ it.id == assessorId }
agePhase.removeFromAssessors(assesor)
Grails文档中的引用:您需要调用
AgePhase
对象上的removfrom
(因为它具有属于此类的属性),并将Assessor对象作为参数
示例:
def agePhase = AgePhase.get(agePhaseId)
def assessor = Assessor.get(assessorId)
agePhase.removeFromAssessors(assessor)
这将删除agePhaseId和assessorId的jointable中的条目难道不像
Assessor.FindallByAgePase(agePhaseId.get(agePhaseId))*。delete()
?在所有Assessor上调用find是一种糟糕的风格,因为您查询数据库中的所有资产。