我正在使用 Hibernate 3 和 MySQL5.5。
我是休眠的新手,我得到以下异常
Exception in thread "main" org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:106)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:152)
我在 hibernate.cfg.xml 文件中设置了 Dialect 属性。我尝试了很多组合
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="DIALECT">org.hibernate.dialect.MySQL5Dialect</property>
实际的属性名称是什么?Hibernate.dialect 还是只有方言?什么是可能的属性值?
我正在添加更多信息,我使用了
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
正如以下答案所建议的那样。
我什至没有构建任何代码,只是试图创建简单的配置:
Configuration cfg = new Configuration().addClass(Employee.class);
sessionFactory = cfg.buildSessionFactory();
下面是实际的配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<!-- Mapping files will go here.... -->
</session-factory>
</hibernate-configuration>
我怀疑没有找到hibernate.cfg.xml?我已将它放在 Employee.class 的源代码所在的同一个包中。事实上,在这个文件中移动会导致同样的错误,所以实际上并没有找到它:-(在哪里保存它?这是一个独立的测试程序:-(