GORM(varchar.save(),非日期):ORA-01861:文本与格式字符串不匹配
您好,我收到一个非常奇怪的错误:ORA-01861:文本与格式字符串不匹配。我所有的网络搜索都与日期问题有关。我的只是保存一个简单的字符串。如果您看到问题,请让我知道如何解决它 专栏GORM(varchar.save(),非日期):ORA-01861:文本与格式字符串不匹配,gorm,Gorm,您好,我收到一个非常奇怪的错误:ORA-01861:文本与格式字符串不匹配。我所有的网络搜索都与日期问题有关。我的只是保存一个简单的字符串。如果您看到问题,请让我知道如何解决它 专栏 TREND: VARCHAR2(31, CHAR), no constraints 在GORM中映射为: String trend ... static constraints = { trend(nullable: true, blank: true) ... } ... static mapping
TREND: VARCHAR2(31, CHAR), no constraints
在GORM中映射为:
String trend
...
static constraints = {
trend(nullable: true, blank: true)
... } ...
static mapping = {
trend column: "TREND", sqlType: "varchar(31)"
...}
发行方式:
def fix_trend(){
// There should ever only be three values found in the trend column
// of the Reports table: "TRENDING UP", "TRENDING STEADY", "TRENDING DOWN"
println "Running groovy database procedure : fix_trend"
StatusReport.list().each{
String trend = it.trend
if (trend != 'TRENDING UP' && trend != 'TRENDING STEADY' && trend != 'TRENDING DOWN'){
trend = trend?.toUpperCase()
if (trend == null){
trend = 'TRENDING STEADY'
}
else if (trend.contains('UP')){
trend = 'TRENDING UP';
}
else if (trend.contains('DOWN')){
trend = 'TRENDING DOWN';
}
else{
trend = 'TRENDING STEADY';
}
it.trend = trend;
it.save();
}
}
println "fix_trend completed"
}
该方法的目的是确保所有值要么呈上升趋势,要么呈下降趋势,要么呈稳定趋势
堆栈跟踪:
Running groovy database procedure : fix_trend
fix_trend completed
Error |
2015-01-05 10:53:02,392 [http-bio-8080-exec-4] ERROR spi.SqlExceptionHelper - ORA-01861: literal does not match format string
Error |
2015-01-05 10:53:02,481 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - SQLDataException occurred when processing request: [GET] /investigator/statusReports/selection
ORA-01861: literal does not match format string
. Stacktrace follows:
Message: ORA-01861: literal does not match format string
Line | Method
->> 439 | processError in oracle.jdbc.driver.T4CTTIoer
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 395 | processError in ''
| 802 | processError . . . . in oracle.jdbc.driver.T4C8Oall
| 436 | receive in oracle.jdbc.driver.T4CTTIfun
| 186 | doRPC . . . . . . . in ''
| 521 | doOALL in oracle.jdbc.driver.T4C8Oall
| 205 | doOall8 . . . . . . in oracle.jdbc.driver.T4CPreparedStatement
| 1008 | executeForRows in ''
| 1307 | doExecuteWithTimeout in oracle.jdbc.driver.OracleStatement
| 3449 | executeInternal in oracle.jdbc.driver.OraclePreparedStatement
| 3530 | executeUpdate . . . in ''
| 1350 | executeUpdate in oracle.jdbc.driver.OraclePreparedStatementWrapper
| 16 | selection . . . . . in investigator.StatusReportsController
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter . . . . . . in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
我解决了自己的问题
表中有一个日期字段在域类中被错误地表示为字符串。即使我试图更新一个VARCHAR2字段,domain_class.save()方法也会更新该domain_类的每个属性——因此错误与趋势字段无关,而是与误报的日期字段相关
我通过添加DataSource.groovy解决了这个问题
logSql = true
formatSql = true
和Config.groovy
log4j.main = {
trace 'org.hibernate.type'
debug 'org.hibernate.SQL'
...
这表明.save()生成的update语句尝试更新每个属性
希望这对将来的人有所帮助。请确保用于插入值的编码由数据库防御使用。我的数据库的NLS_字符集属性为“AL32UTF8”。这是编码错误吗?