Hibernate ASM Spring java.lang.NoSuchMethodError sessionFactory - java

I think i already know what is the problem. i keep getting this error
org.springframework.beans.factory.BeanCreationException:
Could not autowire method: public void proTurism.DAO.AbstractDAO.setSession(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
The problem shoulb be of 2 uncompatible ASM versions one using spring and one hibernate. i have hibernate ASM(unknown version packed in netbeans 7.1) and spring ASM(3.0.6). but i havent found any solution on how to get one asm or anything to get it working in glassfish with netbeans.
my applicationcontext.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="proTurism"/>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

That's a tough dependency problem. You can only solve it if your two libraries (hibernate and spring) depend either on the same version of ASM, or on non-conflicting versions of ASM in terms of the functionality used. If that is not the case, upgrade/downgrade spring/hibernate until it works.
If using Maven, it will automatically show you which artifact requires which versions of its dependencies and it will be easier to trace and play with. Even if not using maven, you can still check the pom definitions of hibernate and spring to see which versions of asm they work with.

Related

spring migration 4 to 5; what is causing java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean

I am migrating an old app from Spring 4 to 5. It builds fine with Maven, but when I start the app in jboss 7.1, I get this error:
java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean
Spring 5.3.20
Hibernate 5.3.28.Final
I have been following this guide, https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x
This said Hibernate 5 is required, so I updated that in the pom to 5.3.28.Final
Here is relevant snippet of spring-context.xml:
<bean id="hibernateSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.mycompany.bean.FooService</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
Running with Java 8.
You need to replace org.springframework.orm.hibernate4.LocalSessionFactoryBean with org.springframework.orm.hibernate5.LocalSessionFactoryBean.
The relevant parts of the migration guide are:
Hibernate support has been upgraded to a Hibernate ORM 5.2+ baseline, with a focus on ORM 5.4.x.
This indicates that the minimum version of Hibernate is now Hibernate 5.2.
and
Packages web.view.tiles2 and orm.hibernate3/hibernate4 dropped.
This indicates that the package your XML config is using (org.springframework.orm.hibernate4) no longer exists. Searching for LocalSessionFactoryBean in the Spring Framework 5.3.22 apidoc shows org.springframework.orm.hibernate5.LocalSessionFactoryBean. Its API seems compatible with your XML definition, so changing the class property should be all that you need to change.

Error creating bean EntityFactoryManager on a new project

So I have a project I want to start work with. The senior developer sent me a zip package of the project and database.sql file for initializing the local database for me to just launch the project. But we are facing the error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/spring-database.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
All entities are exactly the same as in the local database. I have changed the XML file config to match my password and URL. Spend 2 days trying to run the project. Will appreciate any help. Please feel free to ask any questions.
<!-- Configure the entity manager factory bean -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.ProgressDialect</prop>
</props>
</property>
<property name="packagesToScan" value="nl.impressie.grazer.hibernate" />
</bean>
Sounds like hibernate is failing to initialize due to a failure to connect to the DB.
Are you sure you have a Postgresql DB running? Is Spring configured correctly to interface with that DB? (i.e. is the host, port, databaseschema, username and password set?)
If this is a new project, you shouldn't be using any of this XML configuration at all. Instead, create a template project at https://start.spring.io that includes the JPA starter, set your spring.datasource.url, and be running in 2 minutes.

datasource configuration error for Spring 2.5.6 & ojdbc6

I'm trying to config datasource in Spring 2.5.6.
My database is oracle 11g and jdbc driver is ojdbc6.
The following is my configuration:
<bean id="databaseConnectionPool" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="connectionCachingEnabled" value="true"/>
<property name="URL"><value>${jdbc.dburl}</value></property>
<property name="connectionCacheName" value="PSSMST"/>
<property name="user"><value>${jdbc.dbusername}</value></property>
<property name="password"><value>${jdbc.dbpassword}</value></property>
<property name="maxStatements" value="75"/>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">20</prop>
<prop key="MaxLimit">150</prop>
<prop key="InitialLimit">20</prop>
</props>
</property>
</bean>
But when the Tomcat server starts up, I get this message:
Invalid property 'connectionCachingEnabled' of bean class
[oracle.jdbc.pool.OracleDataSource]: Bean property
'connectionCachingEnabled' is not writable or has an invalid setter
method. Does the parameter type of the setter match the return type of
the getter?
That really makes me upset. I checked the OracleDataSource class, of course, the setConnectionCachingEnabled method exists.
Does anybody know how to resolve this?
The probable reason is that you are using an older version of ODBC than intended. Please check lib folder of your application and also check lib folder of Tomcat.

Configuring JDO in Spring 3.1?

I used to have all my DAOs extend the JdoDaoSupport class which is now deprecated in Spring 3.1. I've made my own AbstractJdoDao class which wraps the PersistenceManagerFactory and all the DAOs extend from there. Is that the way I should be doing?
Also in the documentation on JDO, it seems that the direct instantiation of PersistenceManagerFactory is not the default option, but to use LocalPersistenceManagerFactoryBean wrapped in a TransactionAwarePersistenceManagerFactoryProxy. How to properly instantiate these beans and make them work with the Spring's #Transactional annotations.
Here's the persistence-related part of my application context:
<bean id="persistenceManagerFactoryProxy" class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
<property name="targetPersistenceManagerFactory">
<bean class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
<property name="jdoPropertyMap">
<props>
<prop key="javax.jdo.PersistenceManagerFactoryClass">org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory</prop>
<prop key="javax.jdo.option.ConnectionURL">appengine</prop>
<prop key="javax.jdo.option.NontransactionalRead">true</prop>
<prop key="javax.jdo.option.NontransactionalWrite">false</prop>
<prop key="javax.jdo.option.RetainValues">false</prop>
<prop key="javax.jdo.option.DetachAllOnCommit">true</prop>
<prop key="javax.jdo.option.Multithreaded">true</prop>
<prop key="datanucleus.appengine.ignorableMetaDataBehavior">NONE</prop>
</props>
</property>
</bean>
</property>
<property name="allowCreate" value="false" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
<property name="persistenceManagerFactory" ref="persistenceManagerFactoryProxy" />
</bean>
Now when I load a page accessing the data store:
org.springframework.transaction.CannotCreateTransactionException: Could not open JDO PersistenceManager for transaction; nested exception is java.lang.IllegalStateException: No JDO PersistenceManager bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.jdo.JdoTransactionManager.doBegin(JdoTransactionManager.java:369) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at $Proxy15.queryAll(Unknown Source) ~[na:na]
...
Caused by: java.lang.IllegalStateException: No JDO PersistenceManager bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.jdo.PersistenceManagerFactoryUtils.doGetPersistenceManager(PersistenceManagerFactoryUtils.java:153) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy$PersistenceManagerFactoryInvocationHandler.invoke(TransactionAwarePersistenceManagerFactoryProxy.java:159) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at $Proxy13.getPersistenceManager(Unknown Source) ~[na:na]
at org.springframework.orm.jdo.JdoTransactionManager.doBegin(JdoTransactionManager.java:308) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
... 73 common frames omitted
I've got my example project on GitHub. It's using Google App Engine, so either run it via mvn gae:run in Eclipse (with the Google Plugin for Eclipse), first creating an Eclipse project via mvn eclipse:eclipse.
My suggestion would be to use TransactionAwarePersistenceManagerFactoryProxy or SpringPersistenceManagerProxyBean as suggested by the Spring 3.1 documentation. It seems that this is designed to replace the JdoDaoSupport class.
While what you're suggesting in your question of creating your own AbstractJdoDao wrapper will of course eliminate the deprecation warning, my only concern is that you may inadvertently create a situation that's hard for others to maintain as it won't be what they are used to seeing.
On the other hand, I imagine creating your own wrapper is a very fast way to solve your problem...
You should carefully weigh the advantages/disadvantages of using your own wrapper with the advantages/disadvantages of moving forward with the Spring 3.1 way of doing things. In my experience, taking shortcuts can and oftentimes do come back to haunt you in the future.

Correct version of Spring and Hibernate and required dependencies...Exception due to dependencies

Since three days i am trying to get my Spring-Hibernate programs run. i had really tough time finding the involved dependencies due to version difference between hibernate2 and hibernate3.
Finally i was able to run program with following set of dependencies
cglib-nodep-2.1_3.jar
commons-collections.jar
commons-dbcp.jar
commons-pool.jar
commons-logging.jar
dom4j-1.4.jar
ehcache-1.6.0-beta1.jar
hibernate-3.1.3.jar
hsqldb.jar
jta.jar log4j-1.2.9.jar
mysql-connector-java-5.0.8-bin.jar
org.springframework.orm-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
spring-2.5.6.jar
spring-beans-2.0.4.jar
now after two days of efforts when i was able to manage the above mentioned dependencies i tried building similar program but it is throwing following error.I tried online for solution but the solution which i found there is not correct version of spring and hibernate ...Can any one tell me correct cause of exception and also correct version of the Spring and hibernate
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRecordDAO' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
I am also adding my application context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/subhash"/>
<property name="username" value="root"/>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>MyRecord.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="myRecordDAO" class="com.shoesobjects.MyRecordDAOHibernateWithSpring">
<property name="hibernateTemplate"><ref local="hibernateTemplate"/></property>
</bean>
</beans>
a)
Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.
Source:
Spring 3.1.x Reference >
Hibernate
b)
org.springframework.orm-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
spring-2.5.6.jar
spring-beans-2.0.4.jar
Do you really think mixing current pre-release versions (3.1.x) with ancient versions (2.0.4 was released in 2007) is a good idea?
As matt says: use a dependency managements system like Maven, what you are dealing with is jar hell. Here's an article about referencing Spring 3.x artifacts from maven:
Obtaining Spring 3 Artifacts with
Maven
I suggest using a dependency management tool like Maven or Apache Ivy so you don't have to handle sorting through the dependencies and required versions on your own.
Hibernate docs on using with Maven
Got my problem resolved ...As pointed by Sean it was due to mixing different versions of spring and hibernate. Message for other newbies is please use latest version of spring and hibernate.As of Spring 3.0, Spring requires Hibernate 3.2 or later.one should never mix old and new version.
Here's a simple file that worked for me, I used transaction manager and not template.
<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/stackoverflow" />
<property name="user" value="root" />
<property name="password" value="******" />
<property name="maxPoolSize" value="50" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<property name="mappingResources">
<list>
<value>Post.hbm.xml</value>
<value>Tag.hbm.xml</value>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
</value>
<!--
hibernate.connection.provider_class =
org.hibernate.connection.C3P0ConnectionProvider
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=thread
-->
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!--
<property name="dataSource" ref="mySqlDataSource"/>
-->
</bean>
Also, I think the hibernate download package comes with separate folders that indicate what its specific dependencies are. As for the interrelation between hibernate and spring, I think you will have to use some dependency management tool as others have suggested. I do not think maven is a convenient tool. Just don't ever depart from maven's expected project structure and you should be fine.

Categories