Monday, July 1, 2013

SQLException: Field 'id' doesn't have a default

By using the code below in hibernate configuration file will automatically assign a value into ID column without assigning it manually.
    
        
            
        
        ...
        ...
    
Anyhow it does not work as expected and following rubbish being thrown.
    Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
     at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
     at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
     at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:93)
     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:56)
     ... 22 more
What is wrong with my code? According to my colleague, there is nothing wrong in my code, just the column ID in ORDERS table is not declare as auto increment. Thus by alter the table column to auto increment will solve the problem.

No comments: