在Grails Domain类中添加Date字段时出错

时间:2011-12-04 16:25:35

标签: grails groovy

我有三个这样的课程:

度假

包mnm

class Holiday {
    //Date start
    //Date end
    HolidayStatus status
    String justification
    static belongsTo = [ user : User ]
        static constraints = {
        status(nullable:true)
        user(nullable:true)
        //start(nullable:true)
        //end(nullable:true)
        }
}

用户

package mnm

class User {
    String login
    String password
    static hasMany = [ holidays : Holiday ]
        static constraints = {
    }
}

HolidayStatus

package mnm

class HolidayStatus {
    String name
    static belongsTo = [holiday :Holiday ]
        static constraints = {
        name(blank:false)
    }
}

我写了一个像这样的集成测试:

void testWithBelongsTo() {  
        def user1 = new User(login:"anto", password:"secret")
        assert user1.save()
        def holiday1 = new Holiday(justification:"went to trip")
        assert holiday1.save()
        user1.addToHolidays(holiday1)
        assertEquals 1, User.get(user1.id).holidays.size()
        user1.delete()
        assertFalse User.exists(user1.id)
        assertFalse Holiday.exists(holiday1.id)
    } 

它完美无缺,测试正在通过。当我像这样更改我的Holiday课程时:

class Holiday {
    Date start
    Date end
    HolidayStatus status
    String justification
    static belongsTo = [ user : User ]
        static constraints = {
        status(nullable:true)
        user(nullable:true)
        start(nullable:true)
        end(nullable:true)
        }
}

(即添加start, end类型的Date新字段。)

现在,如果我将测试改为这样的话:

void testWithBelongsTo() {  
        def user1 = new User(login:"anto", password:"secret")
        assert user1.save()
        def holiday1 = new Holiday(justification:"went to trip", start: new Date("05/01/2010"),end: new Date("05/01/2011"))
        assert holiday1.save()
        user1.addToHolidays(holiday1)
        assertEquals 1, User.get(user1.id).holidays.size()
        user1.delete()
        assertFalse User.exists(user1.id)
        assertFalse Holiday.exists(holiday1.id)
    } 

我收到的错误是这样的:

could not insert: [mnm.Holiday]; SQL [insert into holiday (id, version, end, justification, start, status_id, user_id) values (null, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [mnm.Holiday]
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [mnm.Holiday]; SQL [insert into holiday (id, version, end, justification, start, status_id, user_id) values (null, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [mnm.Holiday]
    at mnm.HolidayIntegrationTests.testWithBelongsTo(HolidayIntegrationTests.groovy:43)
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [mnm.Holiday]
    at $Proxy11.saveOrUpdate(Unknown Source)
    at mnm.HolidayIntegrationTests.testWithBelongsTo(HolidayIntegrationTests.groovy:43)
    at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:271)
    at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
    at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:228)
    at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:187)
    at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:174)
    at TestApp$_run_closure1.doCall(TestApp.groovy:82)
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
    at gant.Gant.withBuildListeners(Gant.groovy:427)
    at gant.Gant.this$2$withBuildListeners(Gant.groovy)
    at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:415)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.executeTargets(Gant.groovy:590)
    at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: java.sql.SQLException: Table not found in statement [insert into holiday (id, version, end, justification, start, status_id, user_id) values (null, ?, ?, ?, ?, ?, ?)]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313)
    at $Proxy8.prepareStatement(Unknown Source)

这是怎么回事?我犯了哪个错误?这导致其他测试甚至失败!

提前致谢。

1 个答案:

答案 0 :(得分:4)

end可能是数据库查询语言中的关键字。