我有我认为是一个简单的问题,但一直无法解决...出于某种原因,我有一个使用removeFrom*.save()的控制器,它不会抛出任何错误但不会做任何事情.
运行Grails 1.2 Linux/Ubuntu
剥离以下应用程序以重现问题...
我有两个域对象通过create-domain-class - Job(有很多笔记) - 注意(属于Job)
我有3个控制器通过create-controller - JobController(运行脚手架) - NoteController(运行脚手架) - JSONNoteController
JSONNoteController有一个主要方法deleteItem,旨在删除/删除注释.
它做了以下
当我运行此请求时 - 我没有得到任何错误,但似乎jobInstance.removeFromNotes(noteInstance).save()什么都不做,并且不会抛出任何异常等.我怎样才能找到原因?
我附加了一个示例应用程序,它通过BootStrap.groovy添加一些数据.只需运行它 - 您可以通过默认的脚手架视图查看数据.
如果你运行linux,从命令行运行以下GET" http:// localhost:8080/gespm/JSONNote/deleteItem?job.id = 1¬e.id = 2 "
你可以一遍又一遍地运行它,没有任何不同的事情发生.如果您正在运行Windows,也可以将URL粘贴到Web浏览器中.
请帮忙 - 我被困了!代码在这里是链接文本
注意域名
package beachit
class Note
{
Date dateCreated
Date lastUpdated
String note
static belongsTo = Job
static constraints =
{
}
String toString()
{
return note
}
}
Run Code Online (Sandbox Code Playgroud)
工作领域
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
我得到了它的工作......虽然我无法解释为什么.
我在deleteItem中替换了以下行
noteInstance = Note.get(noteId)
Run Code Online (Sandbox Code Playgroud)
以下内容
noteInstance = jobInstance.notes.find { it.id == noteId }
Run Code Online (Sandbox Code Playgroud)
由于某种原因,jobInstance.removeFromNotes使用该方法返回的对象而不是.get使得它更奇怪的是所有其他gorm函数(实际上不确定动态的函数)是否适用于noteInstance.get(noteId)方法.
至少它在工作!!
请参阅此线程:http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html
我建议您为域对象使用基类,如下所示:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这样,任何两个具有相同非空数据库 ID 的对象都将被视为相等。