刚开始学Hibernate,于是在网上找了几个教程学习,按照教程完成程序编辑,但是运行时,一直出现下面的错误:
Exception in thread "main" org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.bromon.zizz.pojo.dao.UserInfoDao.insert(UserInfoDao.java:56)
at org.bromon.zizz.pojo.dao.UserInfoDao.main(UserInfoDao.java:67)
Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'name' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1981)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1388)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4224)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1933)
... 12 more
在网上找了好多解决办法,都是设置数据库编码格式的问题,但是设置之后还是出现问题,找了好久终于发现了问题,问题出现在User.hbm.xml中有这么一句 <generator class="uuid.hex" />,它的意思是将产生一个32位长度的字符串作为主键,而我的主键设置的长度是20,都快哭了,网上的各种教程解析的不清楚对于新手来说真是一种折磨,希望可以帮到同样困惑的新手