方言未设置为hibernate
我正在使用Hibernate3和MySQL5.5 我是一个新手冬眠,我得到以下例外方言未设置为hibernate,hibernate,dialect,Hibernate,Dialect,我正在使用Hibernate3和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:
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文件中设置了方言属性。我试过很多组合
<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>
下面是实际的配置文件
<?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>
org.hibernate.dialogue.mysqldialogue
jdbc:mysql://localhost/test
根
根1234
com.mysql.jdbc.Driver
真的
真的
创造
1.
线
我怀疑找不到hibernate.cfg.xml?我把它放在Employee.class的源代码所在的包中。事实上,在该文件中移动会导致相同的错误,因此实际上找不到:-(将其保存在哪里?这是一个独立的测试程序:-)(第一行或第二行都可以工作。第二种方法,仅使用“方言”是中所示的方式。如果仍然出现该错误,则表明您可能正在进行其他操作。您如何构建SessionFactory?您确定它正在查找您的hibernate.cfg.xml吗
编辑:根据您的更新,您没有告诉Hibernate从配置文件中配置自己。您正在构建自己的配置并使用它。您需要选择一个或另一个。要么执行,要么执行。属性名为Hibernate.dial
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
org.hibernate.dialogue.mysqldialogue
我不知道是否有类似mysql5方言的东西
有关更多详细信息,请参阅。我也面临同样的问题 之前我的代码是:
private final static SessionFactory factory = new Configuration()
.configure().buildSessionFactory();
现在我把它改成了
private final static SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml").buildSessionFactory();
错误消失了。我认为默认情况下,它只查找hibernate.properties文件。问题只是在配置中 configuration类的
configure()
方法可以获取hibernate(hibernate.cfg.xml)
的配置文件路径
只要告诉它路径。我想问题会解决的。在mi案例中,我通过将与hibernate相关的jars lib放入WEB-INF\lib中解决了这个问题。我也遇到了同样的错误-org.hibernate.hibernateeException:没有设置方言。设置hibernate.dial属性 在为持久性类创建实例时,我忘记了编写cfg=cfg.configure();现在运行正常
Configuration cfg=new Configuration();
cfg=cfg.configure();
sfactory=cfg.buildSessionFactory();
谢谢Ryan,似乎找不到.cfg文件:-(我在问题中添加了更多细节。我们需要一个精确的Hibernate版本。“3”表示从~2007开始的任何内容…方言属性值必须是包
org.hibernate.dialogue
中的任何类,您可以在您的版本的相关javadoc中找到可能的类的列表。此外,hibernate.cfg.xml
应该在您的类路径中,这可能意味着一堆位置,因此我们还需要我需要知道您正在使用的构建系统,例如,对于Maven,它将是proj/src/main/resources
Hi,我正在运行Eclipse中的一个简单的公共静态void main()。3.6有MySQL5方言,所以您可能应该使用它,尽管如果您使用InnoDB引擎,也有一种方言。对不起,“simple main()”现在我不会说太多。我能给你的最好答案是,文件需要在你的类路径中。你可以尝试调用configure(file path)
方法,在configuration
对象上调用完整的路径,看看会发生什么……hibernate在XML配置文件中是可选的,快速显示MySQL5方言已经存在很长时间了。@ryan:谢谢你纠正我。并且做了一些进一步的调查,在参考资料中发现了这一点
private final static SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml").buildSessionFactory();
Configuration cfg=new Configuration();
cfg=cfg.configure();
sfactory=cfg.buildSessionFactory();