将grails与遗留数据库一起使用

时间:2012-10-10 07:13:16

标签: oracle grails legacy-database

我遇到了将Grails与旧版Oracle数据库一起使用的问题。我有旧表TARGETTYPES主键文本列TARGETTYPECODE:

CREATE TABLE "TMS"."TARGETTYPES" 
   (    "TARGETTYPECODE" VARCHAR2(100) NOT NULL ENABLE, 
    "TARGETTYPEDESCR" VARCHAR2(255 CHAR), 
    "ACTIVE" CHAR(1) DEFAULT 'Y' NOT NULL ENABLE, 
     CONSTRAINT "TARGETTYPES_PK" PRIMARY KEY ("TARGETTYPECODE")
   )

我创建了grails域类:

package tmsconf

class Targettypes {

    static transients = ['Targettypecode']
    void setTargettypecode(String Targettypecode) {
            id = Targettypecode
    }

    String  getTargettypecode() {
            return Targettypecode
    }

    String targettypedescr
    String active

    static mapping = {
         table 'TARGETTYPES'
         version false
         columns {
             id generator:'assigned', column:"TARGETTYPECODE", type:'text'
         } 
    }

    static constraints = {
        id()
        targettypecode(size: 1..100, blank: false)
        targettypedescr(size: 0..255)
        active(size: 1..1, blank: false)
        id(nullable: true)
    }
    String toString() {
        return "${targettypecode}" 
    }
}

我也创建了控制器类:

package tmsconf

class TargettypesController {
    def scaffold = true
}

应用程序已成功启动。

当我点击链接tmsconf.TargettypesController时,我在控制台中出错:

Error 2012-10-10 10:55:37,243 [http-bio-8080-exec-9] ERROR util.JDBCExceptionReporter  - ORA-00918: column ambiguously defined

| Error 2012-10-10 10:55:37,305 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  - SQLSyntaxErrorException occurred when processing request: [GET] /TMSConf/targettypes/list
ORA-00918: column ambiguously defined
. Stacktrace follows:
Message: ORA-00918: column ambiguously defined

    Line | Method
->>  445 | processError         in oracle.jdbc.driver.T4CTTIoer
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    396 | processError         in     ''
|    879 | processError . . . . in oracle.jdbc.driver.T4C8Oall
|    450 | receive              in oracle.jdbc.driver.T4CTTIfun
|    192 | doRPC . . . . . . .  in     ''
|    531 | doOALL               in oracle.jdbc.driver.T4C8Oall
|    207 | doOall8 . . . . . .  in oracle.jdbc.driver.T4CPreparedStatement
|    884 | executeForDescribe   in     ''
|   1167 | executeMaybeDescribe in oracle.jdbc.driver.OracleStatement
|   1289 | doExecuteWithTimeout in     ''
|   3584 | executeInternal . .  in oracle.jdbc.driver.OraclePreparedStatement
|   3628 | executeQuery         in     ''
|   1493 | executeQuery . . . . in oracle.jdbc.driver.OraclePreparedStatementWrapper
|     96 | executeQuery         in org.apache.commons.dbcp.DelegatingPreparedStatement
|     55 | <init> . . . . . . . in grails.orm.PagedResultList
|     15 | list                 in tmsconf.TargettypesController
|    186 | doFilter . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter             in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run                  in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . . .  in java.lang.Thread

请帮忙,我哪里错了

1 个答案:

答案 0 :(得分:0)

这应该有效:

package tmsconf

class Targettypes {

   String targettypecode
   String targettypedescr
   String active

   static mapping = {
      version false
      id generator: 'assigned', name: 'targettypecode'
   }

   static constraints = {
      targettypecode(size: 1..100, blank: false)
      targettypedescr(size: 0..255, nullable: true)
      active(size: 1..1, blank: false)
   }

   String toString() {
      targettypecode
   }
}

现在可以使用name块中的mapping属性,因此不需要创建一个瞬态get / set对来包装id。

我还删除了表名和列名设置,因为它们被设置为将要使用的内容,并删除type:'text',因为Hibernate知道字段的类型,所以它可以使用它的类型专栏。

另外,我根据您展示的SQL为nullable: true添加了targettypedescr

通常,当您尝试映射到旧数据库时,请使用http://grails.org/doc/latest/ref/Command%20Line/schema-export.html脚本来查看Hibernate认为表格应该是什么样子。调整constraintsmapping块,直到它“足够接近”。