简体   繁体   中英

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. On the database side, column is of CLOB type. Strings shorter than 4000 characters are saved correctly, but longer - not (ORA-01461), despite the @Lob annotation and column definition parameter in @Column annotation. I couldn't find solution for this issue, because all I found relates to Spring JDBC Template, not 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-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.

(...)
@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. I replaced ByteArrayXmlAdapter with ClobXmlAdapter (that was needed to correctly marshal object). For creating CLOB I have used NonContextualLobCreator, and, for serialization - I've wrapped it with SerializableClobProxy. Creating BLOB/CLOB is described here: What is the alternate for deprecated Hibernate.createClob(Reader reader, int length)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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