I’m working with Java 17, Spring Boot 3.3, and SQL Server JDBC driver version 12.10.0.jre11. I’ve run into a weird issue where my string values are being saved as byte array object references instead of actual strings.
Here’s what’s happening:
- Database column:
[RegionCode] nvarchar NOT NULL
- Java value I’m passing: “CA”
- What gets stored in database: “[B@7f31245a”
final var hibernateSession = entityManager.unwrap(Session.class);
hibernateSession.doWork(conn -> {
try (PreparedStatement stmt = conn.prepareStatement("insert into regions (RegionCode) values (?)")) {
for (T item : records) {
stmt.setString(1, item.getRegion());
stmt.addBatch();
}
stmt.executeBatch();
}
});
When I debug the PreparedStatement, the first parameter shows up as AppDTVImpl with jdbcType=VARCHAR, but the value is showing as a byte array with javaType=BYTEARRAY.
I also tried being more explicit with the type but it didn’t work:
stmt.setObject(1, item.getRegion(), Types.VARCHAR);
UPDATE: Found the fix
Turns out the problem was caused by this setting:
dataSource.setUseBulkCopyForBatchInsert(true);
This might be related to having an identity column in my table. The bulk copy API seems to have some issues with string handling. Disabling this fixed the problem, though I wish the JDBC driver handled this more gracefully.