如何在 Hibernate 中使用 Oracle XMLTYPE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3819135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:36:11  来源:igfitidea点击:

How to Oracle XMLTYPE in Hibernate

javaoraclehibernateora-17004

提问by Kiru

One of the column is of the type XMLTYPE in Oracle database. In my application, I want to persist the data and using Hibernate.

其中一列是 Oracle 数据库中的 XMLTYPE 类型。在我的应用程序中,我想保留数据并使用 Hibernate。

I did the following for mapping XMLTYPE in hibernate

我做了以下在休眠中映射 XMLTYPE

  1. Define the custom user type implementing UserType

  2. The custom user type implementation is based on the blog link - http://community.jboss.org/wiki/MappingOracleXmlTypetoDocument

  3. ut we are not using C3p0 connection pool and instead we are using DBCP

  1. 定义实现 UserType 的自定义用户类型

  2. 自定义用户类型实现基于博客链接 - http://community.jboss.org/wiki/MappingOracleXmlTypetoDocument

  3. 我们没有使用 C3p0 连接池,而是使用 DBCP

I am facing issue while creating the XMLType in the custom user type

我在自定义用户类型中创建 XMLType 时遇到问题

XMLType.createXML(st.getConnection(),HibernateXMLType.domToString((Document) value));

where st is the preparedStatement passed to the method.

其中 st 是传递给方法的 prepareStatement。

The connection object returned by st.getConnection() is of the type org.apache.commons.dbcp.PoolableConnection

st.getConnection() 返回连接对象的类型为 org.apache.commons.dbcp.PoolableConnection

But the createXML method expects connection object only of the type oracle.jdbc.OracleConnection

但是 createXML 方法只需要 oracle.jdbc.OracleConnection 类型的连接对象

I also tried getting the getInnermostDelegate but this also does not work.

我也尝试获取 getInnermostDelegate 但这也不起作用。

The XMLTYPE creation methods are in the included jar xdb.jar - will there be any changes depending on the versions included?

XMLTYPE 创建方法在包含的 jar xdb.jar 中 - 根据包含的版本会有任何变化吗?

Thanks

谢谢

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++

Got the SQLConnection object using the below code-

使用以下代码获取 SQLConnection 对象-

SimpleNativeJdbcExtractor extractor = new SimpleNativeJdbcExtractor();
PoolableConnection poolableConnection = (PoolableConnection) extractor
                .getNativeConnection(st.getConnection());
Connection sqlConnection = poolableConnection.getInnermostDelegate();

Now, the error message is java.sql.SQLException: Invalid column type

现在,错误消息是java.sql.SQLException: Invalid column type

Below is the over ridden method

下面是覆盖的方法

public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {

        SimpleNativeJdbcExtractor extractor = new SimpleNativeJdbcExtractor();
        PoolableConnection poolableConnection = (PoolableConnection) extractor
                .getNativeConnection(st.getConnection());
        Connection sqlConnection = poolableConnection.getInnermostDelegate();

        try {
            XMLType xmlType = null;
            if (value != null) {
                xmlType.createXML(sqlConnection, HibernateXMLType
                        .domToString((Document) value));

            }
            st.setObject(index, xmlType);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException(
                    "Could not convert Document to String for storage");
        }
    }

Left with no clue now...

现在一点头绪都没有……

回答by Bassel Kh

I faced a lots of problem using XMLType from java, I fixed this problem is a very simple way.

我在使用 Java 中的 XMLType 时遇到了很多问题,我用一种非常简单的方法解决了这个问题。

I changed the input data type from oracle DB side, from XMLtype to CLOB, then I easily pass CLOB and at the first line in the stored procedure; I built an XMLType from CLOB.

我将输入数据类型从 oracle DB 端从 XMLtype 更改为 CLOB,然后我轻松地通过了 CLOB 并在存储过程的第一行;我从 CLOB 构建了一个 XMLType。

CLOB is very straight forward in java, just use it as String (statement.setString(1, string);)

CLOB 在 Java 中非常直接,只需将其用作 String( statement.setString(1, string);)

回答by Matt M

I solved this same problem, but mapped the XMLType column to java.lang.String in my entity, which simplified the solution.

我解决了同样的问题,但将 XMLType 列映射到实体中的 java.lang.String,这简化了解决方案。

I left a detailed, step-by-step solution as an answer to another question.

我留下了详细的分步解决方案作为另一个问题的答案