Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Abstract Generic Class for DAO with spring SessionFactory injection setup help needed.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am new to spring, and need some help.
Before, I was using hibernate and a hibernateutil along with an abstract generic class for database access.

Here is my class I was using before



Since Im new to spring, I was going a different route at first.
I was just setting up hibernatetransactionmanager in spring to inject a hibernateFactory into my service class. From here i was just accessing the session and doing hql queries for each method as needed in the service class.
This works, but i like having all of my CRUD operations for each hibernate bean ready for me and all I have to do is extend the abstract class
and typecast my hibernate bean to allow myself to minimize code.



I like my last way of using an abstract generic classes, but when I tried this with Spring, I get a nullpointer exception for my sessionFactory that I'm trying to inject

here is the bean setup for spring



Im using the @Transactional transaction annotation at the top of the abstract generic AbstractHibernateRepository class.

Anyone have ideas on the proper way to setup my spring config file?
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your last approach is the approach that I would recommend. The only thing I see is that your class="" attribute in your bean is pointing to an abstract class, an abstract class cannot be instantiated in Java. So your class="" has to point to an actual instantiable class.

Mark
 
David Brave
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I got some advice on how to do this here http://forum.springsource.org/showthread.php?p=350974#post350974

Here are my classes/interfaces









Here is my spring applicationContext.xml setup



Here is my service class.
Ive got a struts2 mvc setup and its accessing the getEvents( ) sevice method.

I also have sessionFactory being injected into my service class. originally this was how i was originally populating the data.
Now when I run the program and eventtRepository is injected into my Services class, I get this error.

2998 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'data.EventRepository' for property 'eventtRepository'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'data.EventRepository' for property 'eventtRepository'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:462)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:499)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:493)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1371)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1330)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 26 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:447)
... 32 more
Mar 15, 2011 9:11:17 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'data.EventRepository' for property 'eventtRepository'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'data.EventRepository' for property 'eventtRepository'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:462)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:499)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:493)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1371)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1330)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 26 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing data.test.IEventsRepository,data.test.IAbstractHibernateRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [data.EventRepository] for property 'eventtRepository': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:447)
... 32 more

_______________________________________________________________________________________

Ive tried using a Main class with a main method such as:



My EventRepository is fetched and it prints out the events in the mysql database.

If I use ClassPathXmlApplicationContext in my service class's getEvents( ) method directly, then I dont get the same error as
I mentioned above, and my service class works fine.
Any idea on what I need to do to allow myself to directly inject my EventRepository directly into my service class without using
the ClassPathXmlApplicationContext.
Anyone know what I'm doing wrong?

thanks.
 
David Brave
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I was making a newbie to Spring mistake.
I was trying to inject the EventRepository into the property of Services EventRepository.
I changed this to the interface that EventRepository uses and changed the setter in Services and it worked.

 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!