我正在尝试创建一个简单的Groovy& Grails与Oracle表的接口(在其他一些列中)包含1列数据类型ANYDATA。 (我是G& G的新手,所以请耐心等待。)
虽然域类很容易让我将列映射到sqltype“sys.anydata”,但控制器似乎无法处理这个问题。我得到了典型的错误: ORA-00932:不一致的数据类型:预期CHAR得到CHAR
由于处理Oracle anydata列可能很棘手,这并不让我感到惊讶,并且可能会有很多grails方面的工作来使其正常运行。
首先,我猜我需要覆盖控制器的save方法,以便将String转换为ANYDATA以成功插入。甚至不知道从哪里开始...通常在oracle SQL中你会使用类似的东西:
INSERT到mytable(id,anydatacol)值(1,sys.anyData.convertVarchar2('mystring'))
在INSERT SQL中获取插入anydata的字符串。
如何自定义控制器以便像上面那样插入(使用字段上的某些功能)而不是固定的保存方法?
我很想看到一个有关grails和anydata的工作示例,但无论我使用带有grails的任何数据有多远,我都很好奇如何自定义如上所述的控制器。
非常感谢,这似乎非常有帮助!
取得一些进展。一切都在编译,但是当我运行app时,我得到:
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并且抛出相同的错误。
我是否需要使用自定义usertype进行其他配置?从您发布的链接中,我创建了一个类。除此之外,我的班级看起来像:
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 = {'user-type'(type:AnyScalarDataUserType,class: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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
答案 0 :(得分:0)
Grails在引擎盖下使用Hibernate。我搜索但未找到Oracle ANYDATA
的映射类型,因此您需要实现custom User Type。基本上它会转换为Hibernate如何从数据库中删除和反转你的列。
Here's a example我发现的UserType与ANYDATA一起使用。
由于您是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])
}
}