Hibernate入门:第一个hibernate程序

来源:互联网 发布:行业大数据平台 编辑:程序博客网 时间:2024/05/18 02:11

虽然了简单的一个入门程序,但是还是出现了不少错误,纠结了一整天,终于将问题一一解决。

问题一:

Configuration config = new Configuration();出错,找不到配置文件,无法初始化,检查配置文件hibernate.cfg内容和目录位置都没错;

在网上找了许多相关错误解决方案,都无果,最后见到一篇文章,说是不使用系统库的hibernate库,自己添加hibernate的jar包,尝试了一下,问题解决了!

 

问题二:

SessionFactory sessionFactory = config.configure().buildSessionFactory( new ServiceRegistryBuilder().buildServiceRegistry());出错,

出现以下异常:

WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Exception in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

检查hibernate.cfg.xml文件无错,有配置dialect,后来在src目录下添加hibernate.properties配置文件,内容如下:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.password=634587
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.username=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect

就可解决问题!

原创粉丝点击