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)
  • 1
  • 2

异常信息说明:不能解析连接池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)
  • 1
  • 2
  • 3

异常信息说明:不能加载mysql驱动 
解决思路:拷贝mysql驱动至classpath或者修改maven的pom.xml

<dependency>
    <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

异常3.密码错误

异常信息摘要:

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

异常信息说明:不能获取连接

解决思路:修改mysql的密码至正确

密码写错或者connection.pasword少写一个s字母
<property name="connection.pasword">12345</property>
  • 1
  • 2

异常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>
  • 1
  • 2
  • 3
  • 4

异常信息说明:url地址或者key写错

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

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

异常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)
  • 1
  • 2
  • 3

异常信息说明:映射异常,hibernate配置文件没有加载映射文件

解决思路:在配置文件中通过mapping元素将映射文件加载进来

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

异常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">
  • 1
  • 2
  • 3
  • 4
  • 5

异常信息说明:映射文件全类名不正确

解决思路:修改全类名

属性package只写包路径,不要将类名也写进去
<hibernate-mapping package="com.jege.hibernate.single.table">
    <class name="User" table="t_user">
  • 1
  • 2
  • 3

异常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" />
  • 1
  • 2
  • 3
  • 4

异常信息说明:映射文件属性名不正确

解决思路:修改属性名

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

异常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());
  • 1
  • 2
  • 3
  • 4

异常信息说明:load方法没有找到对应的主键的实体

解决思路:使用get方法替换load,判断返回值是否null

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

引用原文:https://blog.csdn.net/je_ge/article/details/53875649

写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

猜你喜欢

转载自www.cnblogs.com/summary-2017/p/8965007.html
今日推荐