0

在我的 Hibernate Application 中,有一个字段Active是布尔类型。我的 Hibernate Bean 类专栏

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

在我的服务类书面代码中......

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
4

2 回答 2

0

休眠类型文档

org.hibernate.type.YesNoType

将布尔值映射到 JDBC CHAR 类型为 ('N' | 'n') = false, ('Y' | 'y' ) = true

所以表列类型(tinyint)不适合保存你的值。将其设为 char 或 varchar。

于 2013-03-22T07:54:06.667 回答
0

对于您的问题,这里有两种解决方案,

1,如果类型 ACTIVE 列是 {tinyint},则应在代码中使用短数字,例如。

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

2、如果你必须在你的代码中使用布尔类型,请change the type of ACTIVE column to {char}

祝你好运!

于 2013-03-22T09:02:45.053 回答