1. 程式人生 > >關於Hibernate配置及資料庫訪問過程中報錯及解決辦法

關於Hibernate配置及資料庫訪問過程中報錯及解決辦法

使用Hibernate對MySQL資料庫中的表資料進行增刪改查操作過程中出現的問題及解決辦法:

1. 缺少日誌檔案配置。

解決辦法:首先確認是否添加了log4j-**.jar和self4j-log4j****.jar包,然後在src下新增log4j.properties檔案,其內容

log4j.rootLogger = debug,stdout,D,E
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

2. 上述問題解決後,測試報錯

[WARN ] 2018-06-20 20:31:53,543 method:org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:147)
Could not obtain connection to query metadata
java.sql.SQLException: Unknown system variable 'tx_isolation'
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
	at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
	at com.mysql.jdbc.Connection.getTransactionIsolation(Connection.java:3854)
	at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:138)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:113)
	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
	at cn.itcast.test.CustomerTest.insertTest(CustomerTest.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我所使用的MySQL版本為mysql8.0.11而所使用的驅動版本為mysql-connector-java-5.0.8-bin.jar,接著升級使用mysql-connector-java-8.0.11.jar,並修改hibernate.cfg.xml檔案中的一些配置

<!--連線資料庫的url -->
	<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=UTC
	 </property>

問題得到解決,以下為對customer表增刪改查操作效果。

增:

改:


刪:


誠摯感謝以下幾位大神部落格指路: