• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

SessionFactory Null  RSS feed

 
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm a newbie in the Spring enviroment but I have been reading a lot about how it works.
By now I'm doing a simple project with Hibernate, so here is where things goes interesting.
In the servlet-context file I have something like this:



And I have an abstract class (In order to extend it over all my DAO implementations) that looks like this:



Doing this when I need create a new method, all that I do in my other DAO is create a local variable with type Session and set the getSessionFactory().getCurrentSession().

I have been testing the connection credentials in order to validate the dataSource, anyway always that I try to obtain the Session I got null

What can I be loosing in my configuration?


Reggards!
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any particular reason you are reinventing wheels? Why not just use HibernateDAOSupport?

Can you post your DAO bean? How are you getting an instance of the DAO? I don't see it being declared in your spring context. Is it loaded by Spring. Note that spring autowiring works only in beans loaded by spring. If you are loading your own beans, Spring isn't going to inject dependencies into it.
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayesh!
As I said I'm new with this, right now I'm checking HibernateDAOSupport, some advice about it?

I'm using injection by annotations, in my DAO bean I have something like this:



Can you point me what more I need to put in my servlet-context? As I see comparing to tutorials it is fine, the bean SessionFactory is declared and injected into de GenericDao.

Regards!
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I found this about HibernateDAOSupport http://stackoverflow.com/a/5104965
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an update, until now nothing happens.
Still I don't understand why the SessionFactory is not being injected or initialized.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code where you are calling the dao? and the stack trace?
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayesh, in the other post I show a Dao who extends the GenericDao and the way how I try to call the sessionFactory.
A brief summary about what I'm doing is: creating a bean in the XML using the Spring class org.springframework.orm.hibernate4.LocalSessionFactoryBean, which should create a new session factory object, so in that order I use the Autowired annotation to inject it to my GenericDao, but always getting null, aparently it is not being created.

 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again. Can you post the code where you are calling the DAO. I understand you posted the code for the DAO. Please read this carefully. Post the code that is calling the DAO

Also, snippets of stack trace don;t help. Please post the complete stack trace. All I can see is that it seems you are trying to call the DAO from an Axis web service. I am interested in knowing how you got the instance of the DAO in the Axis web service.
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found something about this, finally I can get the sessionFactory object but whit a weird behavior.
Normally I'm calling the DAO in this way:

Declaring in my controller class a tipe of my Dao

@Autowired
private myDao myDao; // where "myDao" is actually the interface

and then when I need it I create using

myDao = new myDaoImpl();
myDao.myMethod();

What I found is that using the "new" declaration my sessionFactory returns null, but if just calling the method, everything is ok.

But not all is great, because if I use the same code in other class (intermediate class with @Component annotation) the myDao needs to be initializated using new... and then again sessionFactory null.

Don't worry about Axis, in my test I did a web service who is calling the method, the stack trace doesn't say too much, just the NullPointerException.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never use new to create a bean that has Spring annotations. Spring can process the autowire annotation in a bean only if it creates the bean itself. If you create a bean, then all them members with Autowire annotation on them will always be null

Now, if your "intermediate" class has an member of type myDAO with an Autowire annotation, then you need to make sure, you do not do new on that intermediate class. It's hard to tell what exactly you are doing because you refuse to post the code that you are being asked to post.



Please don't tell me that. The stack trace tells a lot more than you think. You don't know how to read the stack trace. It's annoying to ask you the same thing 3 times, and get no response. Or worse, "Oh no.. don;t worry about it". Even the Axis bit is relevant. Because I want to see how you are initializing the DAO, and somehow your Axis web service is getting an instance of the DAO, and I want to see how you are getting the instance.

Really, if you want help you should post the SSCCE
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, but still is not working, getting null when I call my class via Autowired.

This is what I'm calling via Axis



That's it.

Don't get upset if I say that don't worry aout Axis or for what else can be saying the stack trace, you know that when is throwed an exception is a kind of inverse cascade, so it always point to this class with the Null
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you instantiating PageServiceImpl? If you are doing new PageServiceImpl(), then pageDao will be null.
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This @Service is being called from the web service, so I don't do anything similar to new, also if I call the same method from a Controller I still get the same problem, very annoying.
 
Cosme Fulanito
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATE:
I did everything again, calling my method with the Controller now it works, but I found that using the web service don't.
Apparently when calling the WSDL is created an instance and that is making everything Null.

Do you know how can I do this? because in other way I will have to create manually my SessionFactory objet using a kind of Hibernate helper.
 
You can't expect to wield supreme executive power just because
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!