aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate, HibernateUtil.getSession(), returning null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate, HibernateUtil.getSession(), returning null" Watch "hibernate, HibernateUtil.getSession(), returning null" New topic
Author

hibernate, HibernateUtil.getSession(), returning null

Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Hello. Well i'm kind of a newbie on hibernate technology and i'm currently experiencing a problem.
i'll try to explian the best i can.

I have a sql database which schema is called thehiveschema (i can access it through netbeans so it is allright).

So, as far as i know, to consult, update or what else on the DB with hibernate i have to create a session and a transaction. So i have this code:

NUMBER 1


NUMBER 2

note that this code wasn't made by me but i assume it is right because i have searched for several examples online and they look like the same



my hibernate.cfg.xml is like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/thehivischema?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">Neves</property>
<property name="hibernate.connection.password">deisisec</property>

<mapping class="db_interaction.utilizador"></mapping>
</session-factory>
</hibernate-configuration>





Well when I execute number one I get: "Erro no hibernate: null" After debugging it i realized that the problem is on Session s = HibernateUtil.getSession();. Apparently this is null

can somebody helpe me on this?


thanks in advance
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

I think you need to get a Session like this:
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
James Boswell wrote:I think you need to get a Session like this:



And what shoul i return on getsession? :-/
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Pedro

Simply return the Session object which your code already does. I think your problem is how you are creating the SessionFactory, not the Session.
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
James Boswell wrote:Pedro

Simply return the Session object which your code already does. I think your problem is how you are creating the SessionFactory, not the Session.



Hello James. Thanks for the answer. I have now tried like this:





It makes all sense but it does not work to.. I have tried to specify hibernate.cfg.xml configuration file on .configure("/hibernate/HibernateUtils.java/") but still does not work..

I saw a thread on a forum that tells that JRE version and project compilation version may differ (and should not). I already fixed that to but it was not that either.

kind regards ;)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Pedro

What exactly happens when you run the code? What exception (if any) do you receive?
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

hi Pedro,

As I can see in your hbm.cfg.xml, you have used <mapping class="db_interaction.utilizador"></mapping> tag. This leads me to believe that you are using annotations in the class file to map it with corresponding table. When we use annotations to map class to table we use AnnotationConfiguration() not the Configuration() which is ment to be used when mapping is defined in hbm files.

Below code seciton is completely correct and does not need to be changed-

The code secion that you have pasted seems to be correct. I think the problem might be in mapping of utilizador which is preveting the session factory creation.

You are facing issues while debugging because you did not follow the best practices of exception handelling. Exception should be caught only if you can handle it else we should let it pass through. In HibernateUtil you are eating the exception and printing only the abstact of error message which is of not much use.

Considering the fact that buildSessionFactory does not throw any checked excption and we are not even handling un-checked exception, I do not see any reason why you have inclosed session factory creation in try catch block. Please remove it as mentioned in below code section, then you will get the complete trace, which will help you identifying the root cause of error.

If you still face any issues, please post the complete error trace in this forum.







Regards
Salil Verma
Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

I Hope this issue has been resolved..

Actually You got NullPointerException in the initial ocde due to duplicate variable declaration in the try block.
once the static block execution completes, those variables are no more.. hence the static variables remained NULL.


Thanks & Regards
Gopakumar
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hi GopaKumar,

The first code that petro posted does not have duplicate variable declaration in static block. It was like mentioned below.
Why was that breaking ?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate, HibernateUtil.getSession(), returning null