Hibernate常见异常

Posted 云中之歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate常见异常相关的知识,希望对你有一定的参考价值。

 

1.数据库编码问题

主要错误信息 

WARN: SQL Error: 1366, SQLState: 22001 ... ERROR: Data truncation: Incorrect string value: \'\\xE5\\xB9\\xBF\\xE5\\xB7\\x9E...\' for column \'addressDetail\' at row 1

详细错误

  1 Jan 16, 2017 9:40:30 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
  2 INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
  3 Jan 16, 2017 9:40:30 AM org.hibernate.Version logVersion
  4 INFO: HHH000412: Hibernate Core {4.3.11.Final}
  5 Jan 16, 2017 9:40:30 AM org.hibernate.cfg.Environment <clinit>
  6 INFO: HHH000206: hibernate.properties not found
  7 Jan 16, 2017 9:40:30 AM org.hibernate.cfg.Environment buildBytecodeProvider
  8 INFO: HHH000021: Bytecode provider name : javassist
  9 Jan 16, 2017 9:40:30 AM org.hibernate.cfg.Configuration configure
 10 INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
 11 Jan 16, 2017 9:40:30 AM org.hibernate.cfg.Configuration getConfigurationInputStream
 12 INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
 13 Jan 16, 2017 9:40:31 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
 14 WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
 15 Jan 16, 2017 9:40:31 AM org.hibernate.cfg.Configuration doConfigure
 16 INFO: HHH000041: Configured SessionFactory: null
 17 Jan 16, 2017 9:40:31 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
 18 INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
 19 Jan 16, 2017 9:40:31 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
 20 INFO: HHH000046: Connection properties: {user=root, password=****}
 21 Jan 16, 2017 9:40:31 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
 22 INFO: HHH000006: Autocommit mode: false
 23 Jan 16, 2017 9:40:31 AM com.mchange.v2.log.MLog <clinit>
 24 INFO: MLog clients using java 1.4+ standard logging.
 25 Jan 16, 2017 9:40:31 AM com.mchange.v2.c3p0.C3P0Registry banner
 26 INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
 27 Jan 16, 2017 9:40:31 AM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
 28 INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@aa965bf6 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@f311721a [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgeby99l19rgv2u1ixjy4j|-5a89dabc, idleConnectionTestPeriod -> 3000, initialPoolSize -> 1, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 5000, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 1, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@4cd08006 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hgeby99l19rgv2u1ixjy4j|-33394b6a, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hgeby99l19rgv2u1ixjy4j|7826c6fd, numHelperThreads -> 3 ]
 29 Jan 16, 2017 9:40:31 AM org.hibernate.dialect.Dialect <init>
 30 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 31 Jan 16, 2017 9:40:31 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
 32 INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
 33 Jan 16, 2017 9:40:32 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
 34 INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
 35 Jan 16, 2017 9:40:32 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
 36 INFO: HHH000397: Using ASTQueryTranslatorFactory
 37 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
 38 INFO: HHH000228: Running hbm2ddl schema update
 39 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
 40 INFO: HHH000102: Fetching database metadata
 41 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
 42 INFO: HHH000396: Updating schema
 43 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 44 INFO: HHH000262: Table not found: address_inf
 45 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 46 INFO: HHH000262: Table not found: person_address
 47 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 48 INFO: HHH000262: Table not found: person_inf
 49 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 50 INFO: HHH000262: Table not found: address_inf
 51 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 52 INFO: HHH000262: Table not found: person_address
 53 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 54 INFO: HHH000262: Table not found: person_inf
 55 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 56 INFO: HHH000262: Table not found: address_inf
 57 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 58 INFO: HHH000262: Table not found: person_address
 59 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
 60 INFO: HHH000262: Table not found: person_inf
 61 Jan 16, 2017 9:40:32 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
 62 INFO: HHH000232: Schema update complete
 63 Hibernate: insert into person_inf (age, name) values (?, ?)
 64 Hibernate: insert into address_inf (addressDetail) values (?)
 65 Jan 16, 2017 9:40:32 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 66 WARN: SQL Error: 1366, SQLState: 22001
 67 Jan 16, 2017 9:40:32 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 68 ERROR: Data truncation: Incorrect string value: \'\\xE5\\xB9\\xBF\\xE5\\xB7\\x9E...\' for column \'addressDetail\' at row 1
 69 Exception in thread "main" org.hibernate.exception.DataException: could not execute statement
 70     at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:69)
 71     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
 72     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
 73     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
 74     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
 75     at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
 76     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
 77     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
 78     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
 79     at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
 80     at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
 81     at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
 82     at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
 83     at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
 84     at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)
 85     at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
 86     at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
 87     at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
 88     at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
 89     at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
 90     at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
 91     at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
 92     at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
 93     at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
 94     at hql.PersonManager.testPerson(PersonManager.java:45)
 95     at hql.PersonManager.main(PersonManager.java:21)
 96 Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect string value: \'\\xE5\\xB9\\xBF\\xE5\\xB7\\x9E...\' for column \'addressDetail\' at row 1
 97     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4118)
 98     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
 99     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
100     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
101     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
102     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
103     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
104     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
105     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
106     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
107     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
108     ... 21 more
View Code

错误原因及解决办法 

这种问题通常都是因为你的数据库或者表的编码不支持中文,查看mysql中数据库和表的属性就知道,

可以看到当前的编码是拉丁文的,我们需要改为 utf8-general-ci 编码,

当然数据表也需要重建或修改编码,因为之前建的数据表也跟数据库的编码一样了,现在也要改为 utf8-general-ci

之后Hibernate就可以正常工作啦  ^_^

 

 

 

 

 

 

 

 

以上是关于Hibernate常见异常的主要内容,如果未能解决你的问题,请参考以下文章

hibernate+struts+Spring常见异常

hibernate 异常 怎么解决

具有运行时 pojos 的带有 Hibernate 的 OSGi 片段包

常见的代码片段

Alfred常见使用

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段