I am new to hibernate and i am struggling on using it with a tomcat server. this is what I get and code as follow :
Error logs :
GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé
(context initialized) à l'instance de classe d'écoute (listener)
[org.springframework.web.context.ContextLoaderListener]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 50 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at pro.logikal.testdbconnexionspring.configuration.DBConfig.sessionFactory(DBConfig.java:26)
at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$$43adff96.CGLIB$sessionFactory$2(<generated>)
at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$$43adff96$$FastClassBySpringCGLIB$$9b15a99a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$$43adff96.sessionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 51 more
Configuration file :
@Configuration
@EnableTransactionManagement
public class DBConfig {
/**
* Créer la base de données et associe les entités utilisables pendant la
* session
*/
@Bean
public SessionFactory sessionFactory() {
return new LocalSessionFactoryBuilder(getDataSource()).scanPackages("pro.logikal.testdbconnexionspring.entity")
.buildSessionFactory();
}
/**
* Configure l'accès à la base de données. La configuration à la base de données
* se trouve dans le fichier de contexte (WebContent / Meta-inf / Context.xml)
*
* @return Datasource
*/
@Bean
public DataSource getDataSource() {
try {
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("java:/comp/env/jdbc/logdb");
return dataSource;
} catch (NamingException e) {
e.printStackTrace();
return null;
}
}
@Bean
public HibernateTransactionManager hibernateTransactionManager() {
return new HibernateTransactionManager(this.sessionFactory());
}
}
`Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context docBase="TestDBConnexionSpring"
path="TestDBConnexionSpring" reloadable="true"
allowCasualMultiParsing="true">
<Resources cachingAllowed="true" cacheMaxSize="100000"/>
<Resource
name="jdbc/logdb"
auth="Container"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/logdb"
username="root"
password="g5tn4gfs"
maxActive="200"
maxIdle="5"
maxWait="-1"
removeAbandonned="true"
removeAbandonnedTimeout="60"
logAbandonned="true">
</Resource>
</Context>
Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<resource-ref>
<description>DB Connexion</description>
<res-ref-name>jdbc/logdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Can anyone tell me what's wrong. Looks like the db connexion can be made in various ways with spring but I would like to understand why this code is working at work but not when i write the same on my personal computer (ide, spring and hibernate sharing same version and configuration as far as I looked on both environments). here is my pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pro.logikal</groupId>
<artifactId>TestDBConnexionSpring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestDBConnexionSpring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-version>4.3.10.Final</hibernate-version>
<dbcp-version>2.1.1</dbcp-version>
<mysql-version>6.0.5</mysql-version>
<springboot-version>1.4.1.RELEASE</springboot-version>
<spring-version>4.2.0.RELEASE</spring-version>
</properties>
<dependencies>
<!-- Begin Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot-version}</version>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${springboot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${springboot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${springboot-version}</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven.compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<targert>1.8</targert>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
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.