RemoveFrom * does not work without errors

I have what I consider to be a simple problem, but could not solve ... For some reason, I have a controller that uses removeFrom * .save (), which does not cause errors, but does nothing.

Running Grails 1.2 Linux / Ubuntu

The following application is uninstalled to reproduce the problem ...

I have two domain objects through the create-domain class - Work (which has many notes) - Note (which belongs to the task)

I have 3 controllers via create-controller - JobController (forest start) - NoteController (scaffold works) - JSONNoteController

JSONNoteController has one main method deleteItem, the purpose of which is to delete / delete notes.

It performs the following actions

  • checking some queries
  • removes a note from the job - jobInstance.removeFromNotes (noteInstance) .save ()
  • deletes a note - noteInstance.delete ()
  • return status and remaining data as json response.

When I run this query, I get no errors, but it seems that jobInstance.removeFromNotes (noteInstance) .save () does nothing and does not raise any exceptions, etc. How can I track why?

I have attached an example application that adds some data through BootStrap.groovy. Just run it - you can view the default data by default.

If you run linux, from the command line you can run the following GET " http: // localhost: 8080 / gespm / JSONNote / deleteItem? Job.id = 1 & note.id = 2 "

You can run it again and again, and nothing happens. You can also paste the URL into your web browser if you use windows.

Please help - I'm stuck !!! Code here link text

Ad Domain

package beachit class Note { Date dateCreated Date lastUpdated String note static belongsTo = Job static constraints = { } String toString() { return note } } 

Area of โ€‹โ€‹work

 package beachit class Job { Date dateCreated Date lastUpdated Date createDate Date startDate Date completionDate List notes static hasMany = [notes : Note] static constraints = { } String toString() { return createDate.toString() + " " + startDate.toString(); } } 

JSONNoteController

 package beachit import grails.converters.* import java.text.* class JSONNoteController { def test = { render "foobar test" } def index = { redirect(action:listAll,params:params) } // the delete, save and update actions only accept POST requests //static allowedMethods = [delete:'POST', save:'POST', update:'POST'] def getListService = { def message def status def all = Note.list() return all } def getListByJobService(jobId) { def message def status def jobInstance = Job.get(jobId) def all if(jobInstance) { all = jobInstance.notes } else { log.debug("getListByJobService job not found for jobId " + jobId) } return all } def listAll = { def message def status def listView listView = getListService() message = "Done" status = 0 def response = ['message': message, 'status':status, 'list': listView] render response as JSON } def deleteItem = { def jobInstance def noteInstance def message def status def jobId = 0 def noteId = 0 def instance def listView def response try { jobId = Integer.parseInt(params.job?.id) } catch (NumberFormatException ex) { log.debug("deleteItem error in jobId " + params.job?.id) log.debug(ex.getMessage()) } if (jobId && jobId > 0 ) { jobInstance = Job.get(jobId) if(jobInstance) { if (jobInstance.notes) { try { noteId = Integer.parseInt(params.note?.id) } catch (NumberFormatException ex) { log.debug("deleteItem error in noteId " + params.note?.id) log.debug(ex.getMessage()) } log.debug("note id =" + params.note.id) if (noteId && noteId > 0 ) { noteInstance = Note.get(noteId) if (noteInstance) { try { jobInstance.removeFromNotes(noteInstance).save() noteInstance.delete() message = "note ${noteId} deleted" status = 0 } catch(org.springframework.dao.DataIntegrityViolationException e) { message = "Note ${noteId} could not be deleted - references to it exist" status = 1 } /* catch(Exception e) { message = "Some New Error!!!" status = 10 } */ } else { message = "Note not found with id ${noteId}" status = 2 } } else { message = "Couldn't recognise Note id : ${params.note?.id}" status = 3 } } else { message = "No Notes found for Job : ${jobId}" status = 4 } } else { message = "Job not found with id ${jobId}" status = 5 } listView = getListByJobService(jobId) } // if (jobId) else { message = "Couldn't recognise Job id : ${params.job?.id}" status = 6 } response = ['message': message, 'status':status, 'list' : listView] render response as JSON } // deleteNote } 
+4
source share
3 answers

See this topic: http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html

I would recommend using a base class for your domain objects as follows:

 abstract class BaseDomain { @Override boolean equals(o) { if(this.is(o)) return true if(o == null) return false // hibernate creates dynamic subclasses, so // checking o.class == class would fail most of the time if(!o.getClass().isAssignableFrom(getClass()) && !getClass().isAssignableFrom(o.getClass())) return false if(ident() != null) { ident() == o.ident() } else { false } } @Override int hashCode() { ident()?.hashCode() ?: 0 } } 

Thus, any two objects with the same non-zero database identifier will be considered equal.

+3
source

I got his job ... although I canโ€™t explain why.

I replaced the following line in deleteItem

 noteInstance = Note.get(noteId) 

with the following

 noteInstance = jobInstance.notes.find { it.id == noteId } 

For some reason, jobInstance.removeFromNotes works with the object returned by this method, not with .get. What's even weirder is that all the other gorm functions (not sure of dynamic actions) work against the noteInstance.get (noteId )

At least it works though !!

+5
source

I just had the same problem. The removeFrom function was successful, the save was successful, but the physical record in the database was not deleted. Here is what worked for me:

  class BasicProfile { static hasMany = [ post:Post ] } class Post { static belongsTo = [basicProfile:BasicProfile] } class BasicProfileController { ... def someFunction ... BasicProfile profile = BasicProfile.findByUser(user) Post post = profile.post?.find{it.postType == command.postType && it.postStatus == command.postStatus} if (post) { profile.removeFromPost(post) post.delete() } profile.save() } 

So this was a combination of removeFrom, and then deleting in the corresponding domain, and then saving the domain object.

+3
source

Source: https://habr.com/ru/post/1299003/


All Articles