Java 'hibernate.dialect' 必须在没有可用连接错误时设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18717555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'hibernate.dialect' must be set when no Connection available error
提问by star
I am getting the following error when using Hibernate:
使用 Hibernate 时出现以下错误:
'hibernate.dialect' must be set when no Connection available
'hibernate.dialect' must be set when no Connection available
And I am using a datasource for database connection.
我正在使用数据源进行数据库连接。
回答by Arturo Volpe
You need to set the property
您需要设置属性
hibernate.dialect
In the hibernate (persistence.xml or bean declaration) configuration, the value depends on your database, for example:
在 hibernate(persistence.xml 或 bean 声明)配置中,该值取决于您的数据库,例如:
Postgres: org.hibernate.dialect.PostgreSQL82Dialect
Oracle: org.hibernate.dialect.Oracle10gDialect
All posible options are listen here
所有可能的选项都在这里听
For example, a sample persistence.xml looks like:
例如,示例persistence.xml 如下所示:
<persistence-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
回答by emurano
The issue could be that you haven't installed the client library for the database you are trying to connect to.
问题可能是您尚未为尝试连接的数据库安装客户端库。
I have a Spring application that does not have a persistence.xml file and therefore no hibernate.dialect declaration.
我有一个 Spring 应用程序,它没有 persistence.xml 文件,因此没有 hibernate.dialect 声明。
Once I installed the MySQL Connector/Jclient library the error went away.
一旦我安装了MySQL Connector/J客户端库,错误就消失了。
EDIT: I've also gotten this error when the database server wasn't running. Something that often happens now that I run my MySQL server through MAMP.
编辑:当数据库服务器没有运行时,我也收到了这个错误。现在经常发生的事情是我通过 MAMP 运行我的 MySQL 服务器。
回答by Akin Okegbile
This error is hibernate doing a poor job of telling you what went wrong with your attempted connection with database.
这个错误是 hibernate 做得不好,告诉你你尝试连接数据库出了什么问题。
In my case it was as simple as having a wrong password in config file.
就我而言,这就像在配置文件中输入错误密码一样简单。
回答by NewUser
You will get this issue even if you have your configuration files having proper value but you don't configure it in the code.
即使您的配置文件具有适当的值,但您没有在代码中配置它,您也会遇到此问题。
I was explaining hibernate, I forgot to use configure()
before buildSessionFactory()
so I was getting the error.
我正在解释 hibernate,我configure()
之前忘记使用了,buildSessionFactory()
所以我收到了错误消息。
You might want to recheck it.
您可能想重新检查它。
Previous code which was giving me error
以前的代码给我错误
SessionFactory factory = new Configuration().buildSessionFactory();
Changed codeNo Error
更改代码无错误
SessionFactory factory = new Configuration().configure().buildSessionFactory();
回答by bullvinkle
I had this problem too. The reason was missing <property name="dataSource" ref="dataSource" />
element in <bean id="sessionFactory">
definition.
我也有这个问题。原因是定义中缺少<property name="dataSource" ref="dataSource" />
元素<bean id="sessionFactory">
。
回答by Zds
In some cases just using a wrong name for the database results in this Exception. Hibernate is apparently trying to determine the dialect before doing anything else, and as the DB cannot be reached, the error message comes from the part responsible for the dialect select. Bad error messaging on part of Hibernate.
在某些情况下,仅使用错误的数据库名称会导致此异常。Hibernate 显然在做任何其他事情之前试图确定方言,并且由于无法访问数据库,错误消息来自负责方言选择的部分。Hibernate 的部分错误消息。
回答by Sanjay Ingole
Just encountered this issue. In my case it was the hibernate.dialectconfiguration.I added the following to SessionFatcory config in spring context file:
刚遇到这个问题。就我而言,它是hibernate.dialect配置。我在 spring 上下文文件中的 SessionFatcory 配置中添加了以下内容:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.testapp.service.geolocation.LocationData</value>
<value>com.testapp.service.profiles.Profile</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
回答by maihe
In the Hibernate configuration dialog has a tab "options" it is possible to select some. In this case I was using Oracle Enterprise Pack for Eclipsethat already had configured connector, but was still getting the error. Select an option on the list was enough to solve.
在 Hibernate 配置对话框中有一个选项卡“选项”,可以选择一些。在这种情况下,我使用的是已经配置了连接器的Oracle Enterprise Pack for Eclipse,但仍然出现错误。选择列表上的一个选项就足以解决。
回答by toria
I had the same errors.
My problem was that I put the hibernate.properties
under one package
instead of the src
.
So my solution to my problem was moving hibernate.properties
from package
to src
.
我有同样的错误。我的问题是我把hibernate.properties
下一个package
而不是src
. 所以我对我的问题的解决方案是hibernate.properties
从package
到src
.