上周初学hibernate,以下是遇到的一些问题和解决的方法,参考了其他一些前辈的方法经验,大多数是配置文件出的问题
1、Error parsing JNDI name [foo]
这个的原因是在session-factory中定义了name属性,而dtd中没有定义。 我在建立这个xml的时候是使用eclipse的插件建立的,填了一个sessionFactory的name,于是就出现了这个异常。
2、[WARN]: Using org.
hibernate
.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
项目中用户类的主键生成策略,
经查询,Hibernate 3.6开始,如果有model的主键有uuid生成,就会报这个错误,采用最新的生成策略,改成下面的就会正常<generator class="uuid2"/>
3、ERROR: No database selected
要了解的是,hibernate不会帮忙创数据库,得先手工创建数据库才可以
在hibernate.cfg.xml中
<property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_2</property>
此处的spring_hibernate_2这个地方就是设定的数据库名。
|
解决方法:1.数据库是否有'hibernate.ne'这张表。
解决方法: 2.在hibernate.cfg.xml配置文件中加入一行代码
<property name="hibernate.hbm2ddl.auto">update</property>
即设置hibernate为自动更新表
|
5、hibernate.properties not found
把hibernate.properties放到src目录下
6、The method create(EnumSet<TargetType>, Metadata) in the type SchemaExport is not applicable for the arguments (boolean, boolean)
换一种方式使用
7、Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
提示说是数据源配置错误,dialect,driver_class,url,username,password。检查一下这五项
8、 org.hibernate.exception.SQLGrammarException: could not extract ResultSet
自己找到问题了,hibernate配置文件没有配置DDL的生成策略,
<property name="hbm2ddl.auto">create</property>
没有生成表,往表里塞数据的时候就会报找不到表的错误
9、Error executing DDL via JDBC Statement
因为hibernate设置的方言为
org.hibernate.dialect.MySQLInnoDBDialect
该方言建表使用的是MySQL5.0之前的“type=InnoDB”SQL语句,但是使用的MySQL版本为5.7.17,“type=InnoDB”不再生效,变为了“ENGINE=InnoDB”
因此需要将方言设置为 org.hibernate.dialect.MySQL57InnoDBDialect