Duplicar posibles:
Hibernate: different object with the same identifier value was already associated with the sessiongriales "un objeto diferente con el mismo valor de identificador ya se asoció con la sesión" error
tengo el siguiente código en mi regulador en Grails que está fallando con el mensaje de error "a different object with the same identifier value was already associated with the session"
. que ya han visitado algunas páginas donde dice que debo llamar "merge"
antes de llamar a save que termina con este error Provided id of the wrong type for class com.easytha.QuizTag. Expected: class java.lang.Long, got class org.hibernate.action.DelayedPostInsertIdentifier
Alguien ha sugerido que griales plugin de búsqueda podría ser la causa de esto y yo debería eliminar búsquedas = verdadera forma de mi dominio clase que no es una opción (consulte la publicación anterior aquí grails searcheable plugin search in inner hasMany class)
Una cosa a señalar es que el error no se produce en el momento de llamar a q.save() sino que se produce al llamar a redirigir redirigir (acción: " mostrar ", id: id) !!
¿Alguna sugerencia?
def addTags(String tags,Long id){
if(tags){
String[] strTags = tags.split(",");
Quiz q = Quiz.get(id)
for(String t in strTags){
Tag tagToAdd = Tag.findByTag(t)
if(!tagToAdd){
tagToAdd = new Tag(tag:t)
tagToAdd.save()
}
println "---> "+tagToAdd +" Quiz"+q?.id
def qt = QuizTag.findByQuizAndTag(q,tagToAdd)
if(!qt){
qt = new QuizTag(quiz:q,tag:tagToAdd);
q.addToTags(qt)
}
}
q.save()
redirect(action:"show",id:id)
}
}
----------- --------------- EDITAR
Final code that worked with searchable plugin
def addTags(String tags,Long id){
if(tags){
String[] strTags = tags.split(",");
Quiz q = Quiz.get(id)
for(String t in strTags){
if (q.tags.any { QuizTag qt -> qt.tag.tag == t }) { continue; }
Tag tagToAdd = Tag.findOrSaveByTag(t);
QuizTag qt = new QuizTag(quiz:q,tag:tagToAdd)
q.addToTags(qt)
}
q.save(flush:true)
redirect(action:"show",id:id)
}
}
partir de la descripción del método save(): "El objeto no se persistió inmediatamente a menos que se utiliza el argumento ras." Es por eso que el error solo ocurre cuando la solicitud finaliza. –
@TiagoFarias tienes razón. después de llamar a q.save (flush: true) recibo el error en esa línea, otra cosa que hay que tener en cuenta es que incluso después del error, ¡aún se guardan mis datos! Además, este error solo se produce si la etiqueta ya existía, lo que significa que "Tag.findByTag (t)" devuelve algo – Sap