SQL Server byte[] as lob is throwing | SQL Error: 2628, SQLState: S0001 String or binary data would be truncated

Viewed 14

This was previously working, it started failing just today. Below is the issue. I have an table class_A and class_B with below entity definitions. class_b has an Lob called icon. I am getting below exception. The image size is 3.4 Kb. It should fit in the varbinary defined for the icon column in database. This is failing on SQL server.

ClassA implements serializable {

@Id
    @Column(name = "id", columnDefinition = "INT", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

@Column(name = "name")
    private String name;

@OneToOne(mappedBy = "classA", fetch = FetchType.LAZY,  cascade= CascadeType.ALL, orphanRemoval=true)
    private ClassB ClassB;

//getters and setters
}

ClassB implements seriabliable {
@Id
    @Column(name = "id", columnDefinition = "INT", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

@Column(name = "icon")
    @Lob
    private byte[] icon;

@OneToOne(fetch = FetchType.LAZY,  cascade= CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name = "classA_id", referencedColumnName = "id", nullable = false)
    private ClassA classA;
//getters and setters
}

below is the hibernate/spring insert queries generated for insert pretty standard insert statements

SystemOut|Hibernate:
   insert
    into
        class_a
        (name)
    values
        (?);

Exception:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated in table 'dbo.class_b', column 'icon'. Truncated value: ''.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1624)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:594)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:524)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2979)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:248)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:223)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:473)
        at com.rsa.securidaccess.active.jdbcproxy.PreparedStatementProxy.executeUpdate(PreparedStatementProxy.java:141)
        at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197)

I am not sure what I am doing wrong here?

call to save is a standard repository save() method call.

class_b DDL
create table class_b
(
    id                     int identity
        primary key,
    icon                          varbinary,   
    class_a_id int          not null
        constraint fk_class_a_id
            references class_a
)
0 Answers
Related