Does anybody recognize this error?
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'mySessionFactory': Post-processing of the
FactoryBean's object failed; nested exception is
java.lang.SecurityException: Unable to get members for class
org.hibernate.impl.SessionFactoryImpl
Well, I have this Spring3 configuration:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.google.appengine.api.rdbms.AppEngineDriver" />
<property name="url" value="jdbc:google:rdbms://xx:xx/nomedb" />
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>it.trew.prove.model.beans.Scadenza</value>
<value>it.trew.prove.model.beans.Fornitore</value>
<value>it.trew.prove.model.beans.Societa</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!-- <prop key="hibernate.hbm2ddl.import_files">/setup.sql</prop> -->
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
I can't see nothing wrong in this code, I have all dependencies, last versions.
Edit: some of my pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
<type>pom</type>
<!--hibernate-dependencies is a pom, not needed for hibernate-core-->
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- Google App Engine meta-package -->
<dependency>
<groupId>net.kindleit</groupId>
<artifactId>gae-runtime</artifactId>
<version>${gae.version}</version>
<type>pom</type>
</dependency>
While this page deals with Struts, the issue is the same: http://struts.apache.org/2.2.1/docs/google-app-engine-gae.html
Related
I am getting the following exception while fetching data from DB using hibernate in spring mvc.
SEVERE: Servlet.service() for servlet [spring] in context with path [/DemoApp] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
java.lang.NullPointerException: Cannot invoke "java.util.Map.get(Object)" because "this.serverVariables" is null
My chunk of code is below.
Session session = this.sessionFactory.getCurrentSession();
List<User> listUsr = session.createQuery("From User Where username=\'"+usr.getUsername()+"\' and passphare=\'"+usr.getPassphare()+"\'").list();
System.out.println("Lit user "+listUsr);
if(listUsr != null && listUsr.isEmpty()) {
System.out.println("Empty details");
} else {
System.out.println("Value fetched");
}
Sevlet code ..
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mycart?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="PASWORD" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.demoapp.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="connection.autoReconnect">true</prop>
<prop key="connection.autoReconnectForPools">true</prop>
<prop key="connection.is-connection-validation-required">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager" />
<tx:annotation-driven/>
POM snap example...
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
This looks to be driver issue. Please check version of driver and database version.
I was running into this same issue and regardless of research and code additions I could not resolve the issue. I recommend using https://start.spring.io/ to recreate your project adding the Spring Web, Spring Data JPA, and MySQL Driver dependencies. I also recommend this video as it walks through a very simple connection to mysql https://www.youtube.com/watch?v=e8g9eNnFpHQ. This was helpful to me in making sure none of the code I had written was the issue. I can not find any obvious errors in your code but I hope this provides some resolution.
Caused by: java.lang.NullPointerException: null
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
https://github.com/spullara/mysql-connector-java/blob/master/src/main/java/com/mysql/jdbc/ConnectionImpl.java
Upgrade connector , make sure you have latest version of mysql connector
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
I wish to use Hikari Connection Pool in my Spring 4 application. The database is Google Cloud SQL Postgres database.
I have the following dependency in pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
In my applicationContext.xml, I have:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="org.postgresql.Driver" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:postgresql://google/mydatabase?cloudSqlInstance=projectId:regionName:myInstance&socketFactory=com.google.cloud.sql.postgres.SocketFactory</prop>
<prop key="user">postgres</prop>
<prop key="password">mypassword</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
But I'm getting the following exception:
Caused by: java.lang.ClassCastException: Cannot cast org.postgresql.Driver to javax.sql.DataSource
at java.lang.Class.cast(Class.java:3369)
at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:102)
What could be going wrong?
The org.postgresql.jdbc.Driver is not a javax.sql.DataSource, it is a java.sql.Driver, so it doesn't work for the property dataSourceClassName as that property expects a javax.sql.DataSource class name.
If you want to use the driver (and not a DataSource), then you should use the property driverClassName.
So:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="driverClassName" value="org.postgresql.Driver" />
...
Use Hikari recommended datasource for PostgreSQL: org.postgresql.ds.PGSimpleDataSource
<property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
Database Driver DataSource class
PostgreSQL pgjdbc-ng com.impossibl.postgres.jdbc.PGDataSource
PostgreSQL PostgreSQL org.postgresql.ds.PGSimpleDataSource
I use spring data jpa (using CrudRepository interface) in my project and have declared my repository interface like this :
/**
* Repository implementation {#link CrudRepository} for {#link Client} entity
* #author rsone.
*/
public interface ClientRepository extends CrudRepository<Client, Integer> {
}
And have my hibernate config like bellow :
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<context:component-scan base-package="com.rsone.mga.*" />
<jpa:repositories base-package="com.rsone.mga.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="hibernateTransactionManager"/>
<bean id="datasource" parent="abstractDataSource">
<property name="dataSourceName" value="mgaDS" />
<property name="driverClass" value="${database.mga.jdbc.driver}" />
<property name="jdbcUrl" value="${database.mga.jdbc.url}" />
<property name="user" value="${database.mga.jdbc.user}" />
<property name="password" value="${database.mga.jdbc.pass}" />
<property name="preferredTestQuery" value="SELECT 1 FROM DUAL" />
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
<property name="fetchSize" value="5000" />
<property name="cacheQueries" value="true" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes>-->
<property name="packagesToScan" value="com.scor.mga.db"/>
</bean>
<!-- Enables the Hibernate #Transactional programming model -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<!--<property name="generateDdl" value="true"/>-->
<property name="database" value="SQL_SERVER"/>
</bean>
But When I try to run my project it give me that error (knowing that the base package of my repositories is com.rsone.mga.repository) :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<org.springframework.data.repository.Repository<?, ?>>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
Edit
I use version 1.11.0.RELEASE of spring-data-jpa:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.0.RELEASE</version>
</dependency>
I had the same problem in my maven project.
So this below is not working
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
together with
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
Solution
It was solved by updating to the newest Spring boot release :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
Doing this already solved the problem.
It looks like the spring data tags are not backwards compatible with previous versions (other than the managed version).
So I would also recommend removing the version tag from the spring-data-cassandra dependency. Apparently Spring boot 1.5.1.RELEASE manages spring-data-cassandra 1.5.0.RELEASE.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
It doesn't seem to be an error in your configuration files.
It look like you have an #Autowired on a field of type java.lang.Class<org.springframework.data.repository.Repository<?, ?>> in one of the beans that are scanned.
I am new to hibernate and Spring and looking to get a simple example working. Here is my spring xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType">
<bean id="duke" class="com.springinaction.springidol.Juggler">
<constructor-arg value="15" />
</bean>
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
</bean>
<bean id="sonet29" class="com.springinaction.springidol.Sonnet29" />
<bean id="stage" class="com.springinaction.springidol.Stage"
factory-method="getInstance" />
<bean id="saxophone" class="com.springinaction.springidol.Saxophone" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://ec2-21-21-214-251.compute-1.amazonaws.com:5432/d7zxdletq2mmv'" />
<property name="username" value="eqwosdacrklhxpq" />
<property name="password" value="wkAasO8d-hdr2hGoQXYadbjMKhGR" />
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.springinaction.springidol.models" />
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
</bean>
</beans>
My pom.xml dependencies are following
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <!-- Used for Hibernate4 LocalSessionFactoryBean -->
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AOP dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Persistence Management -->
<dependency> <!-- Apache BasicDataSource -->
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency> <!-- Hibernate -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1002-jdbc4</version>
</dependency>
</dependencies>
My application is a simple command line application.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/springinaction/springidol/testing.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
What am i doing wrong. I would like to add that my models directory is empty.
Kind Regards
In the part that you define Hibernate properties, it should be like the following. All keys should be prefixed with hibernate.:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
With this Pom.xml:
<!-- Hibernate resources -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.1.6.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
And With this context xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/x" />
<property name="user" value="x" />
<property name="password" value="x" />
<!-- these are C3P0 properties -->
<property name="acquireIncrement" value="5" />
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="5" />
<property name="maxIdleTime" value="1800" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="src.com.x.model.entities"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- configuration pool via c3p0 -->
<prop key="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.acquire_increment">5</prop>
<prop key="hibernate.c3p0.idle_test_period">1800</prop>
<prop key="hibernate.c3p0.max_size">600</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
after im starting my server i get the following exception:
WARN : org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider - HHH000148: No JDBC Driver class was specified by property hibernate.connection.driver_class
WARN : com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#724ccdfe -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
WARN : com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#724ccdfe -- APPARENT DEADLOCK!!! Complete Status:
what am i doing wrong here?
I'm using spring 3 and hibernate 4,
unfortunately i couldn't find this exact same error message in Google so i guess that means trouble
thanks!
You should not mix these versions. Use:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.2.1.Final</version>
</dependency>
or even better:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernateOrmVersion}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernateOrmVersion}</version>
</dependency>
and define hibernateOrmVersion as a POM property