从Grails连接到Oracle 12c数据库

时间:2014-04-26 14:46:25

标签: oracle hibernate grails

如何配置Grails Web应用程序以连接到简单的(一个表!)Oracle 12c数据库?我已经经历过一系列教程,每一个都不完整或过时。我需要一个简单的,手持式的,逐步的教程。我知道GORM是基于Hibernate的,不知怎的,它都是在引擎盖下完成,但我无法得到一个简单的连接工作。我已经浏览了Grails文档但它似乎更喜欢H2和MySQL连接,而不是Oracle。

所以我明白我必须修改DataSource.groovy,以替换默认的H2设置。以下是我尝试修改Oracle 12c数据库的DataSource.groovy:

dataSource {
    pooled = true
    driverClassName = "oracle.jdbc.driver.OracleDriver"
    username = "scott"
    password = "Sc0ttSc0tt"
    dialect = "org.hibernate.dialect.OracleDialect"
}

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    temp.use_jdbc_metadata_defaults = false
}

// environment specific settings
environments {
    development {
        dataSource {
            pooled - true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = "jdbc:oracle:thin:@192.168.0.105:1521:orcl"
            dbCreate = "validate" // one of 'create', 'create-drop', 'update', 'validate', ''           
        }
    }

    test {
        dataSource {
            pooled = true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = 'jdbc:oracle:thin:@192.168.0.105:1521:orcl'
            dbCreate = 'validate'
        }
    }

    production {
        dataSource {
            pooled = true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = 'jdbc:oracle:thin:@192.168.0.105:1521:orcl'
            dbCreate = 'validate'
        }
    }
}

然后我明白我可以以某种方式使用“脚手架”或GORM或其他任何东西来将域类映射到数据库表...这就是我被困住的地方,并且要么没有喝足够的咖啡或者错过了什么。

有人可以帮忙吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

您使用的是较旧的方言," org.hibernate.dialect.Oracle10gDialect"是你需要的。 (至少对我来说是连接到Oracle11gR2数据库) 您正在使用的方言是针对Oracle9g以及之前的经验。

答案 1 :(得分:0)

在使用Datasource.groovy文件,并通过mwaisgold进行上面推荐的更改之后,我终于得到了以下格式:

dataSource {
    pooled = true
    driverClassName = "oracle.jdbc.OracleDriver"
    username = "scott"
    password = "Sc0ttSc0tt"
}

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    //    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
    temp.use_jdbc_metadata_defaults = false
}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            dialect = "org.hibernate.dialect.Oracle10gDialect"
            url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"           
        }
    }

test {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        dialect = "org.hibernate.dialect.Oracle10gDialect"
        url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"               
    }
}

production {
    dataSource {            
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        dialect = "org.hibernate.dialect.Oracle10gDialect"
        url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"
    }
}

}