【发布时间】:2015-05-22 04:14:25
【问题描述】:
具有以下 grails 配置: 数据源。
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
datasource_staging_oracle {
dbCreate = "none"
url = "jdbc:oracle:thin:@//myoraclehost:1521/DBNAME"
driverClassName = "oracle.jdbc.OracleDriver"
username = "username"
password = "password"
}
}
领域类:
import org.springframework.integration.Message
class SpringMessage {
static mapping = {
datasource 'staging_oracle'
message type: 'blob', column: 'message_bytes'
createdDate type: Date, column: 'created_date'
}
static constraints = {
}
String messageId
Message<?> message
Date createdDate
}
在控制器内部,使用以下方法获取记录:
SpringMessage springMessage = SpringMessage.findByMessageId('messsage_id_value')
以上行失败并出现以下错误: 类 [com.foo.bar.SpringMessage] 上的方法在 Grails 应用程序之外使用。如果在使用模拟 API 或引导 Grails 的测试上下文中正确运行。
如何解决这个问题?谷歌搜索显示 grails“测试”相关的帖子。但这不是测试代码。上面的 findBy 方法是从 grails 控制器调用的。 我在 grails 2.3.3 上,遗憾的是目前无法升级到最新的 grails。
更新
控制器代码:
class FooController {
def index() {
foo2()
}
private def foo2() {
SpringMessage springMessage = SpringMessage.findByMessageId('my_message_id') //This line blowsup
if ( springMessage) {
println springMessage.createdDate
} else {
println "not found"
}
}
}
我使用http://localhost:8080/myapp/foo/index 访问控制器
更新
在我原来的问题中,Blob 列声明不正确。正确版本如下:
class SpringMessage {
static mapping = {
datasource 'staging_oracle'
message type: 'blob', column: 'message_bytes'
createdDate type: Date, column: 'created_date'
}
static constraints = {
}
String messageId
Blob message
Date createdDate
}
【问题讨论】:
-
你能提供控制器实现吗?
-
@saw303 用控制器 sn-p 更新了帖子
-
如何运行 Grails?
-
它来自 Intellijj。使用“run-app”参数运行 grails。
-
您可以从命令行尝试并添加完整的 Stacktrace 吗?从命令行运行 grails run-app
标签: grails grails-orm grails-2.0 grails-domain-class grails-2.3