我尝试使用Spring Data和CRUDRepository接口将CLOB保存到Oracle时遇到问题。在数据库端,列是CLOB类型。尽管@Column注释中的@Lob注释和列定义参数,但正确保存了短于4000个字符的字符串,但更长 - 不是(ORA-01461)。我找不到解决这个问题的方法,因为我发现的所有内容都与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;
}
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>
问题仅在于长于4000的值。
(...)
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "MESSAGE_DATA", columnDefinition = "CLOB NOT NULL")
@XmlJavaTypeAdapter(ByteArrayXmlAdapter.class)
private byte[] messageData;
(...)
答案 0 :(得分:0)
问题已解决。我使用java.sql.Clob来包装该列。我用ClobXmlAdapter替换了ByteArrayXmlAdapter(这是正确编组对象所必需的)。为了创建CLOB,我使用了NonContextualLobCreator,并且,为了序列化 - 我用SerializableClobProxy包装它。这里描述了创建BLOB / CLOB:What is the alternate for deprecated Hibernate.createClob(Reader reader, int length)