我试图为Oracle表创建一个简单的Groovy&Grails接口(interface),该Oracle表(在其他几列中)包含1列数据类型ANYDATA。 (我是G&G的新手,请多多包涵。)
尽管该域类很容易使我将列映射到sqltype“sys.anydata”,但 Controller 似乎无法处理该问题。我得到了典型的错误:
ORA-00932:数据类型不一致:预期的CHAR获得了CHAR
由于处理Oracle anydata列可能很棘手,所以这并不令我感到惊讶,并且可能要进行大量努力才能使其正常运行。
首先,我想我需要重写 Controller 的save方法,以便将String转换为ANYDATA才能成功插入。甚至不确定从何处开始...通常在oracle SQL中,您将使用类似以下内容:
插入mytable(id,anydatacol)值(1,sys.anyData.convertVarchar2('mystring'))
在您的INSERT SQL中将字符串插入到anydata中。
我将如何自定义 Controller 以像上面那样插入(在字段上使用某些功能)而不是 jar 装保存方法?
我很想看到grails和anydata的工作示例,但是无论我将grails和anydata结合使用有多远,我都对如何自定义上述 Controller 感到好奇。
非常感谢,这似乎非常有帮助!
取得一些进展。一切都可以编译,但是当我运行应用程序时,我得到:
Running Grails application
| Error 2013-10-17 22:33:29,123 [startStop-1] ERROR hbm2ddl.SchemaUpdate - could not complete schema update
Message: No Dialect mapping for JDBC type: 2002
Line | Method
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 724 | run in java.lang.Thread
| Server running. Browse to
我的数据库是Oracle 11g,我也在使用方言org.hibernate.dialect.Oracle10gDialect ...也尝试了11,这会引发相同的错误。
使用自定义用户类型是否需要做其他配置?通过您发布的链接,我创建了一个类。除此之外,我的课看起来像:
class Anytest {
String name
AnyScalarData anytdat
static mapping = {
table 'ANYTDAT'
anytdat type:AnyScalarData, sqltype: "sys.anydata"
}
static constraints = {
name(blank:false)
anytdat(nullable:true)
}
}
编辑:
使用以下命令更新了Config.groovy:
grails.gorm.default.mapping = {'用户类型'(类型:AnyScalarDataUserType,类:AnyScalarData)}
但现在得到:
Running Grails application
| Error 2013-10-17 23:01:27,441 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Error evaluating ORM mappings block for domain [anydata_01.Anytest]: No such property: AnyScalarDataUserType for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Error evaluating ORM mappings block for domain [anydata_01.Anytest]: No such property: AnyScalarDataUserType for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
Line | Method
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 724 | run in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Error evaluating ORM mappings block for domain [anydata_01.Anytest]: No such property: AnyScalarDataUserType for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
最佳答案
使用DomainClass:创建自定义UserType
Grails在后台使用Hibernate。我搜索了但没有找到Oracle的ANYDATA
的映射类型,因此您需要实现custom User Type。基本上,它将转换为Hibernate如何从列中检索和检索数据库。
我发现的Here's a example具有与ANYDATA一起使用的UserType。
使用SQL
由于您是Grails的新手,所以值得一提的是,您也可以直接使用SQL,而无需使用域类。服务示例:
class MyService {
def dataSource
void addNewRecord(String data) {
groovy.sql.Sql sql = new Sql(dataSource)
sql.execute("insert into my_table(my_anydata_clomn) values(sys.anyData.convertVarchar2(?))",[data])
}
}