简体   繁体   English

如何使用 Spring Data 将 CLOB 插入 Oracle

[英]How to insert CLOB to Oracle using Spring Data

I have problem with trying to save CLOB to Oracle using Spring Data and CRUDRepository interface.我在尝试使用 Spring Data 和 CRUDRepository 接口将 CLOB 保存到 Oracle 时遇到问题。 On the database side, column is of CLOB type.在数据库方面,列是 CLOB 类型。 Strings shorter than 4000 characters are saved correctly, but longer - not (ORA-01461), despite the @Lob annotation and column definition parameter in @Column annotation.短于 4000 个字符的字符串可以正确保存,但更长 - 不是 (ORA-01461),尽管 @Lob 注释和 @Column 注释中的列定义参数。 I couldn't find solution for this issue, because all I found relates to Spring JDBC Template, not Spring Data.我找不到这个问题的解决方案,因为我发现的所有内容都与 Spring JDBC Template 相关,而不是 Spring Data。

try (ByteArrayInputStream inputStream = new ByteArrayInputStream(messageBody.getBytes(StandardCharsets.UTF_8))) {
        message = (DeadLetterMessage) unmarshaller.unmarshal(new StreamSource(inputStream));
    }

    try {
        message = repository.save(message);
    } catch (Throwable e) {
        log.warn("### Failed to store message in database", e);
        throw e;
    }

Properties in persistence.xml: persistence.xml 中的属性:

<persistence-unit name="deadletter" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.lppsa.integration.camel.dlc.entity.DeadLetterMessage</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.connection.SetBigStringTryClob" value="true"/>
        <property name="hibernate.jdbc.batch_size" value="true"/>
    </properties>
</persistence-unit>

Problem is only with values longer than 4000.问题仅在于值超过 4000。

(...)
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "MESSAGE_DATA", columnDefinition = "CLOB NOT NULL")
@XmlJavaTypeAdapter(ByteArrayXmlAdapter.class)
private byte[] messageData;
(...)

Problem is resolved.问题已解决。 I used java.sql.Clob to wrap that column.我使用 java.sql.Clob 来包装该列。 I replaced ByteArrayXmlAdapter with ClobXmlAdapter (that was needed to correctly marshal object).我用 ClobXmlAdapter 替换了 ByteArrayXmlAdapter(这是正确编组对象所必需的)。 For creating CLOB I have used NonContextualLobCreator, and, for serialization - I've wrapped it with SerializableClobProxy.为了创建 CLOB,我使用了 NonContextualLobCreator,并且,对于序列化 - 我使用 SerializableClobProxy 包装了它。 Creating BLOB/CLOB is described here: What is the alternate for deprecated Hibernate.createClob(Reader reader, int length)此处描述了创建 BLOB/CLOB: What is the alternate for deprecated Hibernate.createClob(Reader reader, int length)

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

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