1. 程式人生 > >Hibernate 菜鳥教程 異常 集錦

Hibernate 菜鳥教程 異常 集錦

異常1.Error parsing JNDI name [foo]

異常資訊摘要:

org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [foo]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)

異常資訊說明:不能解析連線池foo

拷貝官方配置檔案hibernate.cfg.xml的裡面<session-factory name=”foo”>預設包含此foo屬性配置,此異常不過是不影響執行的結果

解決思路:刪除name=”foot”即可

異常2.org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]

異常資訊摘要:

org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]
    at org.hibernate.boot.registry
.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:243) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)

異常資訊說明:不能載入mysql驅動
解決思路:拷貝mysql驅動至classpath或者修改maven的pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>

異常3.密碼錯誤

異常資訊摘要:

org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

異常資訊說明:不能獲取連線

解決思路:修改mysql的密碼至正確

密碼寫錯或者connection.pasword少寫一個s字母
<property name="connection.pasword">12345</property>

異常4.連線資料庫url錯誤

異常資訊摘要:

java.lang.UnsupportedOperationException: The application must supply JDBC connections
<property name="connection.urls">jdbc:mysql:///hibernate</property>
org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:mysql6:///hibernate]
<property name="connection.url">jdbc:mysql:///hibernate</property>

異常資訊說明:url地址或者key寫錯

解決思路:修改url地址或者key

<property name="connection.url">jdbc:mysql:///hibernate</property>

異常5.未知實體類

異常資訊摘要:

org.hibernate.MappingException: Unknown entity: com.jege.hibernate.single.table.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)

異常資訊說明:對映異常,hibernate配置檔案沒有載入對映檔案

解決思路:在配置檔案中通過mapping元素將對映檔案載入進來

<mapping resource="com/jege/hibernate/single/table/User.hbm.xml" />

異常6.全類名寫錯

異常資訊摘要:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/jege/hibernate/single/table/User.hbm.xml
Caused by: org.hibernate.MappingException: class com.jege.hibernate.single.table.User.User not found while looking for property: id
Caused by: java.lang.ClassNotFoundException: com.jege.hibernate.single.table.User.User
<hibernate-mapping package="com.jege.hibernate.single.table.User">
    <class name="User" table="t_user">

異常資訊說明:對映檔案全類名不正確

解決思路:修改全類名

屬性package只寫包路徑,不要將類名也寫進去
<hibernate-mapping package="com.jege.hibernate.single.table">
    <class name="User" table="t_user">

異常7.屬性名寫錯

異常資訊摘要:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/jege/hibernate/single/table/User.hbm.xml
Caused by: org.hibernate.PropertyNotFoundException: field [userUame] not found on com.jege.hibernate.single.table.User

<property name="userUame" column="name" />

異常資訊說明:對映檔案屬性名不正確

解決思路:修改屬性名

<property name="username" column="name" />

異常8.物件不存在

異常資訊摘要:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.jege.hibernate.single.table.User#5]

User user = (User)session.load(User.class, 5L);
System.out.println(user.getPassword());

異常資訊說明:load方法沒有找到對應的主鍵的實體

解決思路:使用get方法替換load,判斷返回值是否null

User user = (User) session.get(User.class, 5L);
if (user != null) {
  System.out.println(user.getPassword());
}

如果覺得我的文章或者程式碼對您有幫助,可以請我喝杯咖啡。您的支援將鼓勵我繼續創作!謝謝!
微信打賞
支付寶打賞