removeFrom *不工作且没有错误

时间:2010-01-21 14:49:47

标签: grails

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

运行 Grails 1.2 Linux / Ubuntu

以下应用程序被删除以重现问题......

我通过create-domain-class有两个域对象 - 工作(有很多笔记) - 注意(属于Job)

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

JSONNoteController有一个主要方法deleteItem,旨在删除/删除注释。

它执行以下操作

  • 一些请求验证
  • 从作业中删除记事 - jobInstance.removeFromNotes(noteInstance).save()
  • 删除注释 - noteInstance.delete()
  • 将状态和剩余数据集作为json响应返回。

当我运行此请求时 - 我没有得到任何错误,但似乎jobInstance.removeFromNotes(noteInstance).save()什么也不做,并且不会抛出任何异常等。 我怎样才能找到原因?

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

如果您运行linux,则可以从命令行运行以下命令 得到“http://localhost:8080/gespm/JSONNote/deleteItem?job.id=1&note.id=2

你可以一遍又一遍地运行它,没有任何不同的事情发生。如果您正在运行Windows,也可以将URL粘贴到Web浏览器中。

请帮忙 - 我被困了! 代码在这里link text

注意域名

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
}

3 个答案:

答案 0 :(得分:5)

我得到了它的工作......虽然我无法解释原因。

我在deleteItem中替换了以下行

noteInstance = Note.get(noteId)

以下

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

由于某种原因,jobInstance.removeFromNotes使用该方法返回的对象而不是.get 令其陌生的是,所有其他gorm函数(实际上不确定动态函数)是否适用于noteInstance.get(noteId)方法。

至少它正在工作!!

答案 1 :(得分:3)

请参阅此主题: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
    }

}

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

答案 2 :(得分:3)

我刚刚遇到同样的问题。 removeFrom功能成功,保存成功,但未删除数据库中的物理记录。这对我有用:

  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()

    }

因此,它是removeFrom的组合,然后是关联域上的删除,然后是域对象的保存。

相关问题