Grails:阻止具有多个关系的两个域类之间的级联关联 [英] Grails: Prevent cascading association between two domain classes with multiple relationships

查看:101
本文介绍了Grails:阻止具有多个关系的两个域类之间的级联关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两个领域类;工作和报价。



工作有很多报价,但工作也有一个公认的报价。接受的报价是可以为空的,只有在特定的报价被用户接受后才能设置。

  class Job {
String title
Quote acceptedQuote
}

class Quote {
工作职位
BigDecimal quoteAmount
}

所得到的表格正是我所需要的(至少在美学上),但是当我去保存报价时会出现问题。
根据我的代码中的逻辑,Quote是成功保存的jobId,但不幸的是,报价的ID作为acceptedQuote保存在Job表中。无论如何阻止这种级联?


  def quoteInstance = new Quote(job:jobInstance, quoteAmount:amount)
if(quoteInstance.save(flush:true)){
render view:'show',model:[quoteInstance:quoteInstance]
break
}

显然jobInstance被传递给Quote构造函数以在Quote表中保存关联,但我不知道以防止报价ID作为接受的报价保存到作业表中。
也许我使用的GORM策略不能满足这些要求?



任何帮助都将非常感谢。

解决方案

这可能不是你正在寻找的,但我实际上将这个模型做了一点改变 - 我会在 Quote domain:

$ p $ class Job {
字符串标题
static hasMany = [引号:引用]
}
$ b $ class class Quote {
static belongsTo = [job:Job]
BigDecimal quoteAmount
接受的布尔值
}

然后您的持久性可能如下所示:

  jobInstance.addToQuotes(new Quote(quoteAmount:123.34,accepted:false))//或true 

,并且不用担心原来的问题。



您也可以添加一个瞬变和它的 getter 作业类以获得接受的引用

  class作业{
字符串标题
静态hasMany = [引用:引用]

静态瞬变= ['acceptedQuote']

引用getAcceptedQuote(){
return Quote.findByJobAndAccepted(this,true)
}

}


Consider two domain classes; Job and Quote.

A Job has many Quotes but a Job also has an accepted quote. The accepted quote is nullable and should only be set once a particular Quote has been accepted by a user. I have the relationships mapped as follows (simplified for the purpose of illustration).

class Job {
    String title
    Quote acceptedQuote   
}

class Quote {
    Job job
    BigDecimal quoteAmount 
}

The resultant tables are exactly what I require (at least aesthetically) but the problem arises when I go and save a Quote. The Quote is saved successfully with a jobId as per the logic in my code but unfortunately the quote's id gets saved in the Job table as the acceptedQuote. Is there anyway to block this cascading association? The code that persists the Quote is fairly basic and looks something like the following;

def quoteInstance = new Quote(job: jobInstance, quoteAmount: amount)
if (quoteInstance.save(flush: true)) {
   render view: 'show', model: [quoteInstance: quoteInstance]
   break
}

Obviously the jobInstance is passed to the Quote constructor to save an association in the Quote table but I do not know how to prevent the quote Id saving to the job table as the accepted quote. Maybe the GORM strategy I am using will not satisfy these requirements?

Any help would be much appreciated.

解决方案

This may not be what you are looking for but I would actually model this a little differently - I would have an accepted flag in the Quote domain:

class Job {
    String title
    static hasMany = [quotes: Quote]
}

class Quote {
    static belongsTo = [job: Job]
    BigDecimal quoteAmount
    Boolean accepted
}

Then your persistence could look like this:

jobInstance.addToQuotes(new Quote(quoteAmount: 123.34, accepted: false)) //or true

and no worries regarding your original problem.

You could also add a transient and its getter to the Job class to get the accepted Quote

class Job {
    String title
    static hasMany = [quotes: Quote]

    static transients = ['acceptedQuote']

    Quote getAcceptedQuote() {
        return Quote.findByJobAndAccepted(this, true)
    }

}

这篇关于Grails:阻止具有多个关系的两个域类之间的级联关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆