简体   繁体   English

如何在 olingo v2 中处理 BLOB 和 CLOB?

[英]How to handle BLOB and CLOB in olingo v2?

Here there is pseudocode about how to handle BLOB and CLOB in olingo jpa. 这里有关于如何在 olingo jpa 中处理 BLOB 和 CLOB 的伪代码。 I added the needed imports to the pseudocode:我将所需的导入添加到伪代码中:

package me;

import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;

import javax.sql.rowset.serial.SerialException;

import org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

public class OnDBWriteContent implements OnJPAWriteContent {

    @Override
    public Blob getJPABlob(byte[] binaryData) throws ODataJPARuntimeException {
        try {
            return new JDBCBlob(binaryData);
        } catch (SerialException e) {
            ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
        } catch (SQLException e) {
            ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
        }
        return null;
    }

    @Override
    public Clob getJPAClob(char[] characterData) throws ODataJPARuntimeException {
        try {
            return new JDBCClob(new String(characterData));
        } catch (SQLException e) {
            ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
        }
        return null;
    }
}

the only problem is I couldn't find any implementation for JDBCBlob and JDBCClob .唯一的问题是我找不到JDBCBlobJDBCClob的任何实现。 Any suggestion about how can I implement them or use some classes?关于如何实现它们或使用某些类的任何建议?

If You are using MySQL it requires an additional ExceptionInterceptor along with the Blob Implementation.如果您使用的是 MySQL,它需要一个额外的ExceptionInterceptor以及 Blob 实现。 You can have a custom implementation of ExceptionInterceptor and use it to initialise the Blob field.您可以拥有ExceptionInterceptor的自定义实现并使用它来初始化 Blob 字段。

The code to achieve it would be as follows实现它的代码如下

import java.sql.Blob;
import java.sql.Clob;
import java.util.Properties;

import org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

import com.mysql.cj.exceptions.ExceptionInterceptor;
import com.mysql.cj.log.Log;

public class CustomOnJPAWriteContent implements OnJPAWriteContent {

    @Override
    public Blob getJPABlob(byte[] binaryData) throws ODataJPARuntimeException {
        return new com.mysql.cj.jdbc.Blob(binaryData, exceptionInterceptor);
    }

    @Override
    public Clob getJPAClob(char[] characterData) throws ODataJPARuntimeException {
        
        return new com.mysql.cj.jdbc.Clob(new String(characterData), exceptionInterceptor);

    }

    ExceptionInterceptor exceptionInterceptor = new ExceptionInterceptor() {

        @Override
        public Exception interceptException(Exception sqlEx) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public ExceptionInterceptor init(Properties props, Log log) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void destroy() {
            // TODO Auto-generated method stub

        }
    };

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM