removeFrom *不工作,没有错误 [英] removeFrom* not working and with no errors

查看:123
本文介绍了removeFrom *不工作,没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我认为是一个简单的问题,但一直无法解决...
出于某种原因,我有一个控制器使用removeFrom * .save(),它不会引发任何错误,但不会执行任何操作。



运行
Grails 1.2
Linux / Ubuntu



以下应用程序被剥离重现问题...

我有两个域对象通过create-domain-class
- Job(有许多注释)
- 注意(它属于Job)

我有3个控制器通过create-controller
- JobController(运行脚手架)
- NoteController(运行脚手架)
- JSONNoteController



JSONNoteController有一个主要方法deleteItem,它旨在删除/删除一个笔记。



它执行以下操作:


  • 一些请求验证

  • 删除笔记 - jobInstance.removeFromNotes(noteInstance).save()

  • 删除笔记 - noteInstance.delete()

  • 返回状态和剩余的数据集作为json响应。



当我运行这个请求时 - 我没有收到任何错误,但是似乎jobInstance.removeFromNotes(noteInstance).save()不会抛出任何异常等
如何追踪为什么?

我附加了一个通过BootStrap.groovy添加一些数据的示例应用程序。
只需运行它 - 您可以通过默认的脚手架视图查看数据。



如果你从命令行运行linux,你可以运行以下
GET http:// localhost:8080 / gespm / JSONNote / deleteItem?j​​ob.id = 1& note.id = 2 < a>



您可以一遍又一遍地运行它,而不会发生任何不同。如果您正在运行Windows,您还可以将网址粘贴到您的浏览器中。



请帮助 - 我卡住了!
代码在这里
链接文本



注意域

 包海滩区

类注意
{

日期日期创建
日期lastUpdated

字符串注释

static belongsTo =作业

静态约束条件=
{
}

字符串toString()
{
返回注释
}
}

作业域

 包beachit 

类工作
{

日期dateCreated
日期lastUpdated

日期createDate
日期startDate
日期完成日期

列表注释

static hasMany = [注意:注意]

静态约束=
{
}

String toString()
{
return createDate.toString()++ star tDate.toString();


$ / code>

JSONNoteController

  package beachit 

import grails.converters。*
import java.text。*
$ b $ class JSONNoteController
{

def test = {renderfoobar test}

def index = {redirect(action:listAll,params:params)}

//删除,保存和更新操作只接受POST请求
// static allowedMethods = [delete:'POST',save:'POST',update:'POST']

def getListService =
{
def message
def status
def all = Note.list()

return all
}
$ b $ def getListByJobService(jobId)
{
def message
def status
$ b def jobInstance = Job.get(jobId)
如果(jobInstance)
{
all = jobInstance.notes
}
else
,则全部清除所有

{
log.debug(找不到jobId的getListByJobService作业+ jobId)
}

全部返回

}

def listAll =
{
def message
def status
def listView

listView = getListService()
message =Done
status = 0

def response = ['message':message,'status':status,'list':listView]
将响应呈现为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)
{
lo g.debug(jobId中的deleteItem错误+ params.job?.id)
log.debug(ex.getMessage())
}

if(jobId& &安培; jobId> 0)
{
jobInstance = Job.get(jobId)

if(jobInstance)
{
if(jobInstance.notes)
{
尝试
{
noteId = Integer.parseInt(params.note?.id)
}
catch(NumberFormatException ex)
{
log.debug(noteId中的deleteItem错误+ params.note?.id)
log.debug(ex.getMessage())
}

log.debug( note id =+ params.note.id)
if(noteId& noteId> 0)
{
noteInstance = Note.get(noteId)
if (noteInstance)
{
尝试
{
jobInstance.removeFromNotes(noteInstance).save()
noteInstance.delete()
$ b $ message =note $ {noteId} deleted
status = 0
}
catch(org.springframework.dao.DataIntegrityViolationException e)
{
message =Note $ {noteId}无法删除 - 引用它存在
status = 1


catch(Exception e)
{
message =一些新错误!!!
status = 10
}
* /
}
else
{
message =注意找不到id {$ noteId}
status = 2
}
}
else
{
message =无法识别注释ID:$ {params.note?.id}
status = 3
}
}
else
{
message =找不到找工作的笔记:$ {jobId}
状态= 4
}
}
其他
{
消息=未找到作业,ID为$ {jobId}
status = 5
}

listView = getListByJobService(jobId)

} // if(jobId)
else
{
message =无法识别作业ID:$ {params.job ?.id}
status = 6
}

response = ['message':message,'status':status,'list':listView]
将响应呈现为JSON

} //删除笔记
}


解决方案

看到这个主题: http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html



我会推荐为你的域对象使用基类,如下所示:

 抽象类BaseDomain {

@Override
boolean equals(o){
if(this.is(o))返回true
if(o == null)返回false
// hibernate创建动态子类,所以
//检查o.class ==类会在大部分时间失败
if(!o.getClass()。isAssignableFrom(getClass())&&
!getClass()。isAssignableFrom(o.getClass()))返回false

if(ident()!= null){
ident()== o.ident ()
} else {
false
}
}

@Override
hashCode(){
ident() ?.hashCode()?:0
}

}

这样,任何两个具有相同非空数据库ID的对象都将被视为相同。


I have what I think is a simple problem but have been unable to solve... For some reason I have a controller that uses removeFrom*.save() which throws no errors but does not do anything.

Running Grails 1.2 Linux/Ubuntu

The following application is stripped down to reproduce the problem...

I have two domain objects via create-domain-class - Job (which has many notes) - Note (which belongs to Job)

I have 3 controllers via create-controller - JobController (running scaffold) - NoteController (running scaffold) - JSONNoteController

JSONNoteController has one primary method deleteItem which aims to remove/delete a note.

It does the following

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

When I run this request - I get no errors but it appears that jobInstance.removeFromNotes(noteInstance).save() does nothing and does not throw any exception etc. How can I track down why??

I've attached a sample application that adds some data via BootStrap.groovy. Just run it - you can view the data via the default scaffold views.

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

You can run it over and over again and nothing different happens. You could also paste the URL into your webbrowser if you're running windows.

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

Note Domain

package beachit

class Note
{

    Date dateCreated
    Date lastUpdated

    String note

    static belongsTo = Job

    static constraints =
    {
    }

    String toString()
    {
        return note
    }
}

Job Domain

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
}

解决方案

See this thread: 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 like this:

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
    }

}

That way, any two objects with the same non-null database id will be considered equal.

这篇关于removeFrom *不工作,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆