hibernate入門會遇到的錯誤 Unable to create requested service
阿新 • • 發佈:2018-12-15
連線不上資料庫 (不一定是出錯在連線資料庫上) Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set 這liang兩個錯可能同時出現
錯誤圖示
可能出現的問題是在
檢查這個檔案(連線資料庫相關程式碼)裡的程式碼有沒有出錯
圖片程式碼 (*看程式碼有沒有出錯) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1. 資料庫相關 --> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置本地事務(No CurrentSessionContext configured!) --> <property name="hibernate.current_session_context_class">thread</property> <!-- 2. 除錯相關 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 3. 新增實體對映檔案 --> <mapping resource="com/zking/entity/user.hbm.xml"/> </session-factory> </hibernate-configuration>
還可能出錯的地方是
hibernate入門測試程式碼
package com.zking.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.zking.entity.User; public class Test { public static void main(String[] args) { Configuration cnf = new Configuration().configure("/hibernate.cfg.xml"); SessionFactory sessionFactory = cnf.buildSessionFactory(); Session session =sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); //新增 // user.setUserName("zhuzhzu"); // user.setUserPwd("123"); // session.save(user); //修改 // user.setId(2); // user.setUserName("23dddf"); // user.setUserPwd("123ew"); // session.update(user); //刪除 // user.setId(3); // session.delete(user); //查詢 user.setId(3); User user2 = session.get(User.class, 2); System.out.println(user2); transaction.commit(); session.close(); } }