Hibernate环境搭建以及测试(maven)

来源:互联网 发布:吉利知豆d1补贴后价格 编辑:程序博客网 时间:2024/06/06 04:15

今天跟着黑马程序员的视频学习了一下Hibernate,试着自己搭建了环境,真是踩了很多坑,记录一下。

mysql的下载和安装


环境:windows64位
1.选择安装版本的会好一些,免安装版本的会有很多的配置,会很麻烦。下载安装版的mysql,
不管你的系统是64还是32位的,下载32的即可
2.一开始选择的是Developer Default,但是总是安装到一半就出错,后来选择full还是一半就出错,最后选择了custom自己选择安装。注意,如果安装sql server的过程中x64不行的话,就选择x86来安装。

Hibernate环境配置

我用的是maven代码管理工具,所以还有很多坑。
1.hibernate.cfg.xml找不到

Cannot find hibernate configuration file when building Hibernate sessionFactory in Hibernate

在网上查到,如果用maven的话,需要在src/main/下面建一个resources文件夹,把hibernate.cfg.xml放入其中。
这里写图片描述
而且还需要 在代码中添加:
这里写图片描述
参考的解决策略如下:

https://stackoverflow.com/questions/36345377/unable-to-create-requested-service-org-hibernate-engine-jdbc-env-spi-jdbcenviro

2.接下来的bug就是mapping文件找不到。

org.hibernate.MappingException: Could not read mappings from resource: com/User.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)

因为我的文件路径是直接copy的qualified name
这里写图片描述
这样是无法找到的,具体的原因我没有深究,去掉mavenproject/src/main/java/,从包名开始写就OK了。
3.下面就是数据库连接不成功的bug了,改用jdbc连接报同样的bug,所以并不是hibernate的原因。

WARNING: Unexpected exception resolving reference
java.sql.SQLException: The server timezone value ‘UTC’ is unrecognized or represents more than one timezone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc timezone value if you want to utilize timezone support.

在网上找到了原因,是因为mysql-connector的版本的问题,改成5.1.39就解决了。mysql-connector-java-5.1.39.jar。
参考的解决策略如下:

https://stackoverflow.com/questions/26515700/mysql-jdbc-driver-5-1-33-time-zone-issue

4.最后的bug来啦:
hibernate生成的SQL语句在Navicat中执行出现错误;

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Hibernate: create table t_user(
…..
)
发现是hibernate里的dialect和Mysql的版本不匹配

MySql5.0之前的配置:

 <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

5.0之后需要使用:

<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

参考如下:

http://www.cnblogs.com/lycsky/p/6087794.html

原创粉丝点击