1

在我的应用程序中,用户进行搜索。该搜索会返回一份可以被聘用的人员名单。但是,用户可以对同一个职责进行多次搜索(例如,我需要两个人来这个城市,三个人来那个城市)。每次搜索都会产生一个“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
4

2 回答 2

2

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

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

然后,当您保存 cocon 时,它指向与 prep 相同的集合。看看这个链接,它说的是同样的事情。

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

也可以帮助你。

于 2012-10-24T14:22:22.973 回答
0

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

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

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

于 2012-10-25T07:15:43.390 回答