In my app, the user makes a search. That search gives back a list of people that can be hired for a duty. But, user can make more than one search for the same duty (say, i need two persons for this city, and three for that city). Every search makes a “Prepresupuesto”, and every Prepresupuesto makes a “Cocontrato”. All “Cocontrato” makes a “Contrato”, and all “Prepresupuestos” makes a single “Presupuesto”. And all together conforms the entire duty, called “Campaign” in the app.
When i am trying to save that “Cocontrato”, I receive the exception. This is the code:
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])
}
Method 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])
}
}
The Exception is launched in the if.
Any help? I am a bit lost, I am a newcomer to Grails, and I think i don’t understand completely the exception.
Thank you!
EDIT: The Exception:
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
You’re probably trying to save more than one entity (cocon) when both refer to the same collection. It’s hard to detect that in this code you posted and it could be somewhere else, but I would say it’s when you do this:
Then when you save cocon, it points to the same collection that prep does. Take a look at this link, that says the same thing.
A way out of this would be to add element by element to the list you want to copy to instead of sharing the reference by doing
a.list = b.list.This can also help you.