【问题标题】:Found shared references to a collection in Grails在 Grails 中找到对集合的共享引用
【发布时间】:2012-10-14 09:42:04
【问题描述】:

在我的应用中,用户进行了搜索。该搜索会返回一份可以被聘用的人员名单。但是,用户可以对相同的职责进行多次搜索(例如,我需要两个人来这个城市,三个人来那个城市)。每次搜索都会产生一个“Prepresupuesto”,每个 Prepresupuesto 都会产生一个“Cocontrato”。所有的“Cocontrato”都构成一个“Contrato”,所有的“Prepresupuestos”都构成一个单一的“Presupuesto”。所有这些都符合整个职责,在应用程序中称为“活动”。

当我试图保存那个“Cocontrato”时,我收到了异常。这是代码:

ArrayList<Prepresupuesto> prepresus=Prepresupuesto.findAllByCamp(campid)

        Presupuesto pres=new Presupuesto()

        for(int i=0;i<prepresus.size();i++){

            pres.prepresu.add(prepresus[i])

            pres.camp=Campaign.get(prepres.camp.id)

            pres.estado="Pending"

            total=total+prepresus[i].total

            crearCocontrato(prepresus[i])
            }

方法 crearCocontrato():

public crearCocontrato(Prepresupuesto prep){
    Set <Azafata>azas=prep.azafatas
    Cocontrato cocon=new Cocontrato()
    cocon.contrato=con
    cocon.precio=prep.precio
    cocon.azafatas=azas
    cocon.total=prep.total
    cocon.horas=prep.horas
    cocon.horario=prep.horario
    cocon.localidad=prep.localidad
    cocon.fechaInicio=prep.fechaInicio
    cocon.fechaFin=prep.fechaFin
    cocon.presu=prep
    cocon.camp=prep.camp



    if(!(cocon.save(flush:true))){
        render (view:"fallos", model:[azafataInstance:cocon])

    }
}

异常在 if 中启动。

有什么帮助吗?我有点迷茫,我是Grails的新手,我认为我不完全理解异常。

谢谢!

编辑:例外:

Found shared references to a collection: com.publidirecta.Cocontrato.azafatas.     Stacktrace follows:
Message: Found shared references to a collection: com.publidirecta.Cocontrato.azafatas
Line | Method
->> 2448 | crearCocontrato in com.publidirecta.EntidadController$$ENmku0D2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   2394 | boton           in     ''
|    886 | runTask . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
|    908 | run             in     ''
^    680 | run . . . . . . in java.lang.Thread

【问题讨论】:

  • 请也发布您的异常消息。

标签: grails


【解决方案1】:

当两者都引用同一个集合时,您可能会尝试保存多个实体(cocon)。很难在您发布的这段代码中检测到它可能在其他地方,但我会说是在您执行此操作时:

Set <Azafata>azas=prep.azafatas
cocon.presu=prep

然后,当您保存 cocon 时,它会指向与 prep 相同的集合。看看这个link,它说了同样的话。

解决此问题的一种方法是将逐个元素添加到要复制到的列表中,而不是通过 a.list = b.list 共享引用。

This也可以帮到你。

【讨论】:

    【解决方案2】:

    是的,Tiago,在深入研究了代码(它是继承的代码......)之后,我发现问题出在你所说的。我通过这样做解决了它:

    ArrayList <Azafata> azas=new ArrayList<Azafata>()
        for(int i=0;i<prep.azafatas.size(); i++){
            azas.add(prep.azafatas.toArray()[i])
    } 
    

    一切都很好 :) 谢谢你的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 2010-12-14
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2019-10-24
      相关资源
      最近更新 更多