我将一个grails(2.2.4)应用程序部署到JBoss(5.2)服务器,并为Oracle DB设置了数据库:
datasource {
dbCreate = 'update'
jndiName = 'java:XXX
}
我还有两个域对象:
class A {
def name
static hasOne = [b:B]
static constraints = {
b unique: true
name unique: true
}
}
class B {
A a
static belongsTo = [A]
}
最后是一个用于查找/创建A:
实例的服务A createA(String name) {
def a = A.findByName(name)
if(!a) {
a = new A(name: name)
a.b = new B(a: a)
a.save() <-- This causes the ERROR. Tried a.save(flush:true), a.save(failOnError:true) and a.save(flush:true, failOnError:true)
}
return a
}
当使用Hibernates自己的H2数据库并使用 grails run-app 和 grails run-war 进行本地测试时,这项工作正常,但在与Oracle DB集成并部署之后到JBoss服务器我收到以下错误:
Hibernate operation: could not execute query; uncategorized SQLException for SQL [
select this_.id as id1_0_, this_.version as version1_0_, this_.name as name1_0_
from a this_
where this_.id=?];
SQL state [99999]; error code [17041];
Missing IN or OUT parameter at index:: 1;
nested exception is java.sql.SQLException: Missing IN or OUT parameter at index:: 1
任何人都知道这里出了什么问题?
答案 0 :(得分:1)
考虑到您可以更改域类,我会对您的域类进行以下更改。
class A {
def name
static hasOne = [b:B]
static constraints = {
//b unique: true // try commenting this line out
name unique: true
}
}
class B {
A a
// static belongsTo = [A] // I don't think you need this.
}
在您的服务上,
A createA(String name) {
def a = A.findByName(name)
if(!a) {
a = new A(name: name).save(flush:true, failOnError:true)
//a.b = new B(a: a) // this feels going around in circles.
new B(a: a).save(flush:true, failOnError:true)
// you may only need one save() and the changes will cascade.
//I will leave that upto you which save() cascades and which one doesn't.
}
return a
}
此外,您可以查看此http://docs.grails.org/2.3.1/ref/Domain%20Classes/findOrCreateBy.html以简化您的逻辑。
答案 1 :(得分:0)
我设法解决了这个问题。我必须将unique
attributt放在one-to-one
映射的子关系中,如下所示:
class A {
def name
static hasOne = [b:B]
static constraints = {
// b unique: true <-- REMOVED
name unique: true
}
}
class B {
A a
static belongsTo = [A]
static constraints = {
a unique: true // <-- ADDED
}
}
不确定原因,但确实有效。