hibernate/hibernate-orm · error · JDBCException
Unable to set NCLOB string after creation
Error message
Unable to set NCLOB string after creation
What it means
StandardLobCreator.createNClob(String) obtains an empty NClob through Connection.createNClob() (via LobCreationContext) and then fills it with nclob.setString(1, string). Any SQLException from that flow is wrapped as JDBCException 'Unable to set NCLOB string after creation'.
Solutions
- Prefer mapping the field as @Nationalized String and let Hibernate choose NonContextualLobCreator (stream wrapping) instead of creating NClobs by hand
- Test Connection.createNClob() + setString() directly on your driver to confirm support and capture the real SQLException
- Upgrade the JDBC driver / database if the NClob write path is buggy
- Ensure the Session/connection stays open until flush completes
Example fix
// before: manual NClob creation entity.setNote(session.getLobHelper().createNClob(bigText)); // after: let Hibernate manage nationalized text @Nationalized String note; // on the entity
Defensive patterns
Strategy: try-catch
Validate before calling
// prove driver NClob support once at startup
try (Connection c = dataSource.getConnection()) {
NClob n = c.createNClob();
n.setString(1, "ping");
n.free();
} Try / catch
try {
entity.setNote(lobHelper.createNClob(text));
}
catch (JDBCException e) {
// fall back to plain String / @Nationalized mapping
entity.setNote(text);
} Prevention
- Prefer @Nationalized String attributes over manual NClob creation
- Validate driver NCLOB support in CI against the real DB
- Keep the owning session open until flush
When it happens
Trigger: Calling Hibernate.getLobHelper().createNClob(String), or persisting a nationalized String that the dialect maps to connection-created NClob, on a driver/DB where createNClob() or NClob.setString() fails: connection already closed, NCLOB support absent, or driver bugs.
Common situations: Drivers with partial national-character support; pooled connections that get validated/closed between LOB creation and setString; databases whose national charset is not configured.
Related errors
- Could not create JDBC NClob
- Blobs may not be accessed after serialization
- Can't deal with Blobs larger than 'Integer.MAX_VALUE'
- Can't deal with Clobs larger than 'Integer.MAX_VALUE'
- Clobs may not be accessed after serialization
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/d2ffee767b33a3e4.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/StandardLobCreator.java:51
/**
* Create the basic contextual NCLOB reference.
*
* @return The created NCLOB reference.
*/
public NClob createNClob() {
return lobCreationContext.fromContext( CREATE_NCLOB_CALLBACK );
}
@Override
public NClob createNClob(String string) {
try {
final NClob nclob = createNClob();
nclob.setString( 1, string );
return nclob;
}
catch ( SQLException e ) {
throw new JDBCException( "Unable to set NCLOB string after creation", e );
}
}
@Override
public NClob createNClob(Reader reader, long length) {
// IMPL NOTE : it is inefficient to use JDBC LOB locator creation to create a LOB
// backed by a given stream. So just wrap the stream (which is what the NonContextualLobCreator does).
return NonContextualLobCreator.INSTANCE.createNClob( reader, length );
}
}
View on GitHub (pinned to fad1729dce)