java - Grails save() 方法中的 SQLException

标签 java oracle hibernate grails jboss

我有一个 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/

相关文章:

java - hibernate 困惑。 ga、GA 和 Final Releases 之间有什么区别?兼容性?存储库?

java - 在 Java 中恢复 SSL X509TrustManager

java - 如何通过双击删除行?

linux - 提交后启动时Docker Oracle数据库失败

oracle - 如何在 Oracle 中重新创建 SAP 查询?

java.sql.SQLException : ORA-01403: no data found with OJDBC6

java - 复合键,其中部分键来自父类(super class)

java - Hibernate 中同一个表中的一对多映射

java - 处理长时间运行的 JEditorPane setText 进程的最佳实践

Java - .putAll( t ) 放入 HashMap 中也会将 t 放入另一个 HashMap 中