2011-12-04 57 views
0

我有三個類是這樣的:錯誤而Grails領域類中添加日期字段

假日

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

這裏發生了什麼?我犯了什麼錯誤?這導致其他測試甚至失敗!

在此先感謝。

回答

4

end可能是數據庫查詢語言中的關鍵字。

+0

+1你的正確!謝謝....... –