I am getting the following error when using Hibernate:
'hibernate.dialect' must be set when no Connection available
And I am using a datasource for database connection.
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.
Once I installed the MySQL Connector/J client library the error went away.
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.
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.
You might want to recheck it.
Previous code which was giving me error
SessionFactory factory = new Configuration().buildSessionFactory();
Changed code No Error
SessionFactory factory = new Configuration().configure().buildSessionFactory();
This error is hibernate doing a poor job of telling you what went wrong with your attempted connection with database.
In my case it was as simple as having a wrong password in config file.
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:
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-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
Just encountered this issue. In my case it was the hibernate.dialect configuration.I added the following to SessionFatcory config in spring context file:
<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>
I had this problem too. The reason was missing <property name="dataSource" ref="dataSource" /> element in <bean id="sessionFactory"> definition.
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.
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 Eclipse that already had configured connector, but was still getting the error. Select an option on the list was enough to solve.
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.
Related
In my spring project, i am using Hibernate to export my entity classes to a previously created database. But this will require the final user knows how to create a database in the Database manager system (Currently I am using Postgresql).
Is there any way of, given only the machine where the postgresql is installed (and the username and password, which is provided when the application is runned the first time), the Hibernate create a new database in the server if it doesn't exist?
If your configuration looks like this
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://host:port/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Then the database will be created by Hibernate automatically.
Update:
Ok now I understand what you want. You want to start the Postgresql server with Hibernate. This is not possible. Hibernate does not do this.
You can do this with
Another script that starts with your application
A maven/ant target.
A build job
But the best solution is to use an in-memory database that does not need an external server (for example H2, or Java derby)
See also
Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
and
Postgres database create if not exists
Take a look of paramater hibernate.hbm2ddl.auto for your hibernate.cfg.xml file. I suggest you this link: Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?
Run "CREATE DATABASE ..." (see http://www.postgresql.org/docs/9.0/static/sql-createdatabase.html) as a native SQL query ...
.createSQLQuery(" ... ").executeUpdate(); ...
Hibernate will - at least as far as I know - not create the database, only the tables in it.
I suppose you need to connect to postgresql via a second persistence unit/connection, because of the chicken-and-egg nature of this approach.
I am using spring 3.1 with spring profiles to load the beans. In my app context file, I load the properties like :
<context:property-placeholder order="1" location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/>
And then I use the property value to load the data source bean like
<property name="driverClassName" value="${database.driverClassName}"/>
It works fine.
The problem starts when I add a couple of more property placeholders so that properties from some database tables can be loaded.
This uses a properties reference loaded by
<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="globalSystemConfiguration"/>
</bean>
To add to the details, this configFactoryBean uses the datasource to load the properties from the database.
When I do this, I have the following exception:
java.lang.ClassNotFoundException: ${database.driverClassName}
My analysis is that its trying to load the datasource before resolving the property from the first context property placeholder. I may be wrong. Or maybe spring profile variable is not resolved properly.
Can anyone please help me to fix this.
Thanks
Akki
This bug about multiple property placeholders might relate to your problem: https://jira.spring.io/browse/SPR-9989
When using multiple PropertyPlaceholderConfigurer in conjunction with
#Value annotation and default value for placeholders syntax (ie
${key:defaultValue}), only the first PropertyPlaceholderConfigurer is
used. If this configurer does not contain the desired value, it falls
back to #Value default even if the second
PropertyPlaceholderConfigurer contains the value.
Affects Version/s: 3.1.3
Each <context:property-placeholder> creates a new instance of PropertyPlaceholderConfigurer - it gets messy easily. You should have one such thing per application and on application level, not on libraries' one - that makes maintenance much easier.
For more details and a suggestion how to cope with it look here:
http://rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html
In my application I am using property-placeholder configurer in following way and it works very well. You can try that.
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:META-INF/spring/*_${spring.profiles.active}.properties</value>
</list>
</property>
</bean>
I think this should resolve your problem. :)
Since you have suggested hardcoding the path to the configuration file works, try using the profiles attribute on the tag to selectively include the configuration.
<beans profile="profileName">
<context:property-placeholder order="1" location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>
<beans profile="profileName2">
<context:property-placeholder order="1" location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>
See this article explaining profiles: http://java.dzone.com/articles/using-spring-profiles-xml
I'm (still) having loads of issues with HSQLdb & OpenJPA.
Exception in thread "main" <openjpa-1.2.0-r422266:683325 fatal store error> org.apache.openjpa.persistence.RollbackException: user lacks privilege or object not found: OPENJPA_SEQUENCE_TABLE {SELECT SEQUENCE_VALUE FROM PUBLIC.OPENJPA_SEQUENCE_TABLE WHERE ID = ?} [code=-5501, state=42501]
at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
at model_layer.EntityManagerHelper.commit(EntityManagerHelper.java:46)
at HSQLdb_mvn_openJPA_autoTables.App.main(App.java:23)
The HSQLdb is running as a server process, bound to port 9001 at my local machine. The user is SA. It's configured as follows:
<persistence-unit name="HSQLdb_mvn_openJPA_autoTablesPU"
transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>model_layer.Testobjekt</class>
<class>model_layer.AbstractTestobjekt</class>
<properties>
<property name="openjpa.ConnectionUserName" value="SA" />
<property name="openjpa.ConnectionPassword" value=""/>
<property name="openjpa.ConnectionDriverName"
value="org.hsqldb.jdbc.JDBCDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:hsqldb:hsql://localhost:9001/mydb" />
<!--
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
-->
</properties>
</persistence-unit>
I have made a successful connection with my ORM layer. I can create and connect to my EntityManager.
However each time I use
EntityManagerHelper.commit();
It fail with that error, which makes no sense to me. SA is the Standard Admin user I used to create the table. It should be able to persist as this user into hsqldb.
edit: after hours of debugging I found out why this fails. This kind of error message also appears if you do not set required table entries (NOT NULL). It didn't indicate that for me. It seems the OpenJPA layer mistakes not being able to insert statements because of missing entries for permission problems. I simply accepted the first answer therefore. Thanks for reading :)
I have the impressoin that HSQL has no rights to write its datafile in the configured directory.
That happens to me all the time when I test my server manually as root/Administrator and that when starting it as a daemon/service it changes to a less privileged user. Then the files are owned by another user as the server is running as.
It could be other reasons : on Windows I had it when another process (another server instance) was still clinging on to the files, or even when eclipse in its infinite wisdom decided to index the database.
I'm having trouble starting a transaction with Hibernate and MySQL while running in JUnit. I'm getting a HibernateException which states: "No TransactionManagerLookup specified". I believe this error is because I don't have a proper configuration setting for hibernate.transaction.manager_lookup_class.
I see that under the namespace of org.hibernate.transaction there are quite a few different lookup classes that I could use. All of the documentation that I could find on these was very vague. My question is what is the appropriate one for MySQL?
I do it with Spring and its transaction managers. Works perfectly.
To fix this I needed to make the following changes.
Changed the hibernate.cfg.xml => hibernate.current_session_context_class from jta to thread.
Changed the transaction manager to
org.springframework.orm.hibernate3.HibernateTransactionManager
in the bean configuration.
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" >
<property>
<bean>
I am new to openJPA.
I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml.
For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.
And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.
FYI I am using WS app. server 7 and RAD 7.5
Any type of help would be highly appreciated.
You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.
Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.
Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.
For example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:#localhost:1521:XE
Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.
Of course, you can use JNDI data sources with Spring also.