This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Spring and the fly likes Issue in annotating DAO using spring 3.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Issue in annotating DAO using spring 3.0" Watch "Issue in annotating DAO using spring 3.0" New topic
Author

Issue in annotating DAO using spring 3.0

Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
Hi,

I am using Spring 3.0+Hibernate in my application. This is the error I am getting while I try to annotate my DAO class.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chatSessionBO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mercer.chat.app.dao.ChatSessionDAO com.mercer.chat.app.businessmodel.ChatSessionBO.chatSessionDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chatSessionDAO' defined in file [D:\opt\jboss-4.0.3SP1\server\mercerhr\deploy\mercerhr.ear\mercer.war\WEB-INF\classes\com\mercer\chat\app\dao\ChatSessionDAO.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required







paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 169
Hi ,
I'd look at how your components are accessng the Hibernate Session.

After brief look, it appears that retrieveSessionInfo() in ChatSessionDAO
is used by ChatSessionBO but ChatSessionDAO dosn't have a Session with it despite using a Hibernate Criteria .

That's my best guess.

-Paul
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
Thanks for the reply Paul. Any ideas how can I make sure that I get the Session ?
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 169
MercerDAO needs a HibernateTemplate to be set.
Perhaps move the HibernateTemplate in that class's excecuteFind() method to a class level private variable .
You could then set it in the Spring config file via injection.

Alternatively, if you pass in a HibernateSession via the constructor or injection ,it will create a default HibernateTemplate for you.

Check this out:

http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/orm/hibernate/support/HibernateDaoSupport.html

Hope this helps,
Paul
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
I just checked something out :

In our application the DAO now are being injected like this. Foe example the ServicesPageDAO, this DAO also extends MercerDAO.
So that implies, what is being done by using xml, needs to be done using annotations. I need to inject sessionFactory that is referring mySessionFactory. How do I inject this mySessionFactory using annotations.

paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 169
You can mix and match Annotations and injection using the config file .
I don't use annotations or @Autowired a lot so I don't know the syntax for using annotations with a Session.




Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199
    
    1

You can autowire it in your ServicesDAO as follows -



paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 169
Quick and painless .
Cheers.
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
Thanks for the response Sai. But when I put :


in my code as shown below


I get the following error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chatSessionBO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mercer.chat.app.dao.ChatSessionDAO com.mercer.chat.app.businessmodel.ChatSessionBO.chatSessionDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chatSessionDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.LocalSessionFactoryBean com.mercer.chat.app.dao.ChatSessionDAO.mySessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.orm.hibernate3.LocalSessionFactoryBean] is defined: expected single matching bean but found 3: [&myMarsSessionFactory, &mySessionFactory, &myLogSessionFactory]
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
Hi Thanks for all the help Paul and Sai. I have been able to resolve the issue by providing a @Qualifier.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue in annotating DAO using spring 3.0