hibernate - 为什么在Grails中使用此Spock规范会导致 'identifier of an instance of projectname.Event was altered from 1 to 2;'?

标签 hibernate exception grails integration-testing spock

运行test-app integration:spock会导致以下错误:

identifier of an instance of projectname.Event was altered from 1 to 2; nested exception is org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2

org.springframework.orm.hibernate3.HibernateSystemException: identifier of an instance of projectname.Event was altered from 1 to 2;

nested exception is org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2 at projectname.EventControllerSpec.save: an event(EventControllerSpec.groovy:74)

Caused by: org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2


  • 这个问题源于何处?
  • 如何解决?
  • [OPTIONAL]有人可以创建标签“spock”吗?


  • 来自EventControllerSpec.groovy的
    def "save: an event"() {
        given: "Constraint-conform event properties"
        def eventTitle              = "Being in Beijing"
        controller.params.title     = eventTitle
        controller.params.details   = "Details"
        controller.params.location  = "Beijing"
        controller.params.startDate = "01.09.2030"
        controller.params.startTime = "20:15"
        controller.params.endDate   = "01.09.2030"
        controller.params.endTime   = "21:45"
        controller.params.publisher = getUserObject("someuser")
    
        when: "I save that event"
        def result = controller.save() // THIS IS LINE #74 AS STATED IN THE ERROR
    
        then: "The event is successfully saved and the show-view rendered"
        controller.flash.message.args.grep(eventTitle)
        redirectArgs.action             == "show"
        redirectArgs.id                 == result.eventInstance.id
    }
    
    private User getUserObject(String name) {
        def user = User.findByUsername(name)
    
        if (!user) {
            user = new User()
            user.username = name
            user.email = "${name}@example.com"
            user.pw = "barbar"
            user.pwConfirmation = "barbar"
            assert user.save()
        }
    
        user
    }
    

    来自EventController.groovy的
    def save = {
        def eventInstance = new Event()
    
        eventInstance.title     = params.title
        eventInstance.details   = params.details
        eventInstance.location  = params.location
        eventInstance.startDate = DateUtil.createDate(params.startDate, params.startTime)
        eventInstance.endDate   = DateUtil.createDate(params.endDate, params.endTime)
        eventInstance.publisher = session.user
    
        if (eventInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.title])}"
            redirect(action: "show", id: eventInstance.id)
        }
        else {
            eventInstance.errors.each { log.warn it }
            render(view: "add", model: [eventInstance: eventInstance])
        }
    }
    

    最佳答案

    您的Spock测试会扩展到什么类(class)?应该是spock.lang.Specificationgrails.plugin.spock.IntegrationSpec而非grails.plugin.spock.ControllerSpec,后者是为单元测试而设计的。

    关于hibernate - 为什么在Grails中使用此Spock规范会导致 'identifier of an instance of projectname.Event was altered from 1 to 2;'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768172/

    相关文章:

    spring - 我应该在使用@Transactional 时使用 beginTransaction 吗?

    python - 依赖异常是一种不好的形式吗?

    java - 如何在java中制作像枚举一样的键值

    c++ - 从 std::call_once 抛出异常

    java - "finally"子句在异常处理中的好处

    angularjs - 使用 Angular js 在 Grails 应用程序中上传文件

    hibernate - 同时对 hibernate 和 mongodb 域类进行 Elasticsearch - Grails

    grails - 如何在 Grails 3.x 中配置 JNDI 数据源?

    java - Hibernate 对象映射导致 SQL 错误 (errno : 150) with Liquibase

    java - 如何使用 java.sql.Connection 创建 hibernate 实体管理器