gpt4 book ai didi

java - Grails save() 方法中的 SQLException

转载 作者:行者123 更新时间:2023-12-01 09:21:40 26 4
gpt4 key购买 nike

我有一个 grails (2.2.4) 应用程序部署到 JBoss (5.2) 服务器,并为 Oracle 数据库设置了数据库:

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 DB 并使用 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

有人知道这里出了什么问题吗?

最佳答案

考虑到您可以更改域类,我将对您的域类进行以下更改。

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简化你的逻辑。

关于java - Grails save() 方法中的 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com