2013-03-22 56 views
0

在我的Hibernate應用程序中有一個字段Active它是布爾類型。 我休眠Bean類列在mysql數據庫中使用tinyint列的Hibernate句柄?

@Column(name = "ACTIVE") 
@Type(type = "org.hibernate.type.YesNoType") 
private boolean active; 
//Setter and getter methods 

在我的服務類writig代碼...

public void createUser(UserVO userVO) throws Exception { 
    -------- 
    userVO.setActive(false);//setting false.. 
    ---- 
    ------//Database insert Code hear.. 
} 

但我發現了異常聽到..

Caused by: java.sql.BatchUpdateException: Incorrect integer value: 'N' for column 'ACTIVE' at row 1 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2045) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1468) 
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
    ... 50 more 
Caused by: java.sql.SQLException: Incorrect integer value: 'N' for column 'ACTIVE' at row 1 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444) 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1997) 
    ... 53 more 

回答

0

您的問題這裏有兩種解決方案,

1,如果該類型ACTIVE列{} TINYINT,你應該在你的代碼 如使用短號碼。

userVO.setActive(Short.valueOf("0")); 

2,如果你在你的代碼中使用布爾類型,請change the type of ACTIVE column to {char}

祝你好運!

0

hibernate types docs

org。 hibernate.type.YesNoType

將布爾值映射爲JDBC CHAR類型as('N'| 'N')= FALSE,( 'Y' | 'Y' )=真

所以表列類型(TINYINT)不適合握住你的價值。使其成爲char或varchar。

相關問題