org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
First remove all of your configuration Spring Boot will start it for you.
Make sure you have an application.properties
in your classpath and add the following properties.
spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1
spring.datasource.username=klebermo
spring.datasource.password=123
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create
If you really need access to a SessionFactory
and that is basically for the same datasource, then you can do the following (which is also documented here although for XML, not JavaConfig).
@Configuration
public class HibernateConfig {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}
That way you have both an EntityManagerFactory
and a SessionFactory
.
UPDATE: As of Hibernate 5 the SessionFactory
actually extends the EntityManagerFactory
. So to obtain a SessionFactory
you can simply cast the EntityManagerFactory
to it or use the unwrap
method to get one.
public class SomeHibernateRepository {
@PersistenceUnit
private EntityManagerFactory emf;
protected SessionFactory getSessionFactory() {
return emf.unwrap(SessionFactory.class);
}
}
Assuming you have a class with a main
method with @EnableAutoConfiguration
you don't need the @EnableTransactionManagement
annotation, as that will be enabled by Spring Boot for you. A basic application class in the com.spring.app
package should be enough.
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Something like that should be enough to have all your classes (including entities and Spring Data based repositories) detected.
UPDATE: These annotations can be replaced with a single @SpringBootApplication
in more recent versions of Spring Boot.
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
I would also suggest removing the commons-dbcp
dependency as that would allow Spring Boot to configure the faster and more robust HikariCP
implementation.
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set.
Adding the following line to the properties
file solve this problem when I had that error.
spring.jpa.database=mysql
Spring boot Hibernate error Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set when working with multiple data sources
I figure it out.
Modify method entityManagerFactory
for both Db2Configuration
and OracleConfiguration
to supply them with the information about hibernate dialect:
for DB2
@Primary
@Bean(name = "db2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder
, @Qualifier("db2DataSource") DataSource dataSource) {
final HashMap<String, Object> hibernateProperties = new HashMap<String, Object>();
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.DB2390Dialect");
return builder
.dataSource(dataSource)
.packages("project.dataconfig.db2.entity")
.properties(hibernateProperties)
.persistenceUnit("db2persistanceunit")
.build();
}
for Oracle
@Bean(name = "oracleEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder
, @Qualifier("oracleDataSource") DataSource dataSource) {
final HashMap<String, Object> hibernateProperties = new HashMap<String, Object>();
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
return builder
.dataSource(dataSource)
.packages("project.dataconfig.oracle.entity")
.properties(hibernateProperties)
.persistenceUnit("oraclepersistanceunit")
.build();
}
After this, my console shows when running app, indicating all is good:
HHH000400: Using dialect: org.hibernate.dialect.DB2390Dialect
Initialized JPA EntityManagerFactory for persistence unit 'db2persistanceunit'
HHH000204: Processing PersistenceUnitInfo [name: oraclepersistanceunit]
HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Initialized JPA EntityManagerFactory for persistence unit 'oraclepersistanceunit'
My actuator /health endpoint also sees both databases as up and running.
Exception : org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Hibernate Dialect is a Class which provide pre-defined configurations as per the database you are using.
You need to set a Hibernate Dialect in order to hibernate to identify the database which it going to deal with. You haven't mention which hibernate version you are using and this link does include latest almost common Dialects. This is a sample Hibernate Configuration file (hibernate.cfg.xml
)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:jboss/datasources/MySQLDS</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.jdbc.batch_size">50</property>
</session-factory>
</hibernate-configuration>
You can see hibernate.dialect
set as org.hibernate.dialect.MySQL5InnoDBDialect
since I'm using MySQL 5 InnoDB Engine.
Even if you're configuring hibernate programmatically you have set hibernate.dialect
.
Update
This documentation quite a bit of old, but you'll find what you want.
Related Topics
Selenium 2.53 Not Working on Firefox 47
Junit 5: How to Assert an Exception Is Thrown
How to Get the Current Time in Yyyy-Mm-Dd Hh:Mi:Sec.Millisecond Format in Java
Java Reverse an Int Value Without Using Array
How to Return a JSON Object from a Java Servlet
How to Correctly Get Image from 'Resources' Folder in Netbeans
Is It Good Practice to Use Java.Lang.String.Intern()
How to Get the Intersection Between Two Arrays as a New Array
How to Change Jdk Version for an Eclipse Project
How to Fix the "Java.Security.Cert.Certificateexception: No Subject Alternative Names Present" Error
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
How to Read Integer Value from the Standard Input in Java
Classpath Resource Not Found When Running as Jar