我有一个我认为很简单的问题,但无法解决......
由于某种原因,我有一个使用removeFrom*.save() 的控制器,它不会抛出任何错误,但不会执行任何操作。
跑步
Grails 1.2
Linux/Ubuntu
删除以下应用程序以重现该问题......
我通过 create-domain-class 有两个域对象
- 工作(有很多笔记)
- 注释(属于工作)
我通过 create-controller 有 3 个控制器
- JobController(运行脚手架)
- NoteController(运行脚手架)
- JSONNoteController
JSONNoteController 有一个主要方法deleteItem,旨在删除/删除注释。
它执行以下操作
- 一些请求验证
- 从作业中删除注释 - jobInstance.removeFromNotes(noteInstance).save()
- 删除注释 - noteInstance.delete()
- 以 json 响应的形式返回状态和剩余数据集。
当我运行此请求时 - 我没有收到任何错误,但似乎 jobInstance.removeFromNotes(noteInstance).save() 不执行任何操作,也不会引发任何异常等。
我怎样才能找到原因?
我附上了一个示例应用程序,它通过 BootStrap.groovy 添加一些数据。
只需运行它 - 您就可以通过默认的脚手架视图查看数据。
如果您运行 Linux,则可以从命令行运行以下命令
得到 ”"
你可以一遍又一遍地运行它,不会发生任何不同的情况。如果您运行的是 Windows,您还可以将该 URL 粘贴到您的网络浏览器中。
请帮助 - 我被困住了!
代码在这里链接文本 http://old.nabble.com/file/p27255806/gespmtest.tar.gz
注释域
package beachit
class Note
{
Date dateCreated
Date lastUpdated
String note
static belongsTo = Job
static constraints =
{
}
String toString()
{
return note
}
}
工作领域
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
}