Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 从双返回单字符中选择“字符串”_Oracle_Hibernate_Groovy - Fatal编程技术网

Oracle 从双返回单字符中选择“字符串”

Oracle 从双返回单字符中选择“字符串”,oracle,hibernate,groovy,Oracle,Hibernate,Groovy,我有一个非常简单的Groovy类,我只是尝试通过Hibernate 3.3.2 GA使用本机SQL查询从Oracle 11g数据库中选择一个字符串。它看起来很简单,但我无法理解我得到的结果。代码如下: package serialize import org.hibernate.cfg.Configuration import org.hibernate.transform.ToListResultTransformer class SerializeDatabaseObjects {

我有一个非常简单的Groovy类,我只是尝试通过Hibernate 3.3.2 GA使用本机SQL查询从Oracle 11g数据库中选择一个字符串。它看起来很简单,但我无法理解我得到的结果。代码如下:

package serialize

import org.hibernate.cfg.Configuration
import org.hibernate.transform.ToListResultTransformer

class SerializeDatabaseObjects {


    static main(args) {

        def sessionFactory = initHibernate()

        def session = sessionFactory.openSession()

        def tx
        def result

        try {
            tx = session.beginTransaction()

             result = session
                .createSQLQuery("""Select 'Hello World!' from dual""")
                .setResultTransformer(ToListResultTransformer.INSTANCE) 
                .list()

            tx.commit()
        }
        catch (Exception e) {
            if (tx!=null){ 
                tx.rollback() 
            }
            throw e
        }
        finally {
            session.close()
        }


        println result

        sessionFactory.close()
    }

    static initHibernate(){
        return new Configuration().configure().setProperty("hibernate.show_sql", "true").buildSessionFactory()
    }
}
输出:

Hibernate: Select 'Hello World!' from dual
[[H]]
ToListResultTransformer是我最近一次尝试获取整个“Hello World!”字符串可以打印,但不需要IMO。但无论我尝试什么,H总是结果


如何返回整个“Hello World”字符串?

当我键入此问题时,我找到了答案。Hibernate似乎将结果解释为字符而不是字符串,因此只返回“H”

我发现解决这个问题的方法是在select语句中添加一个列别名,然后使用它指定结果应解释为字符串

同样,我使用了ResultTransformer,因此我的结果将包含列名及其内容,但这并不是严格必需的

如果有人想详细说明为什么Hibernate将结果映射为字符而不是字符串,或者建议另一种更简单的方法将结果强制为字符串,请发表评论或给出答案

以下是生成预期结果的代码:

package serialize

import org.hibernate.cfg.Configuration
import org.hibernate.transform.AliasToEntityMapResultTransformer
import org.hibernate.type.StringType

class SerializeDatabaseObjects {


    static main(args) {

        def sessionFactory = initHibernate()

        def session = sessionFactory.openSession()

        def tx
        def result

        try {
            tx = session.beginTransaction()

             result = session
                .createSQLQuery("""Select 'Hello World!' as hello from dual""")
                .addScalar("hello", new StringType())
                .setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
                .list()

            tx.commit()
        }
        catch (Exception e) {
            if (tx!=null){ 
                tx.rollback() 
            }
            throw e
        }
        finally {
            session.close()
        }


        println result

        sessionFactory.close()
    }

    static initHibernate(){
        return new Configuration().configure().setProperty("hibernate.show_sql", "true").buildSessionFactory()
    }
}
以及输出:

Hibernate: Select 'Hello World!' as hello from dual
[[hello:Hello World!]]