• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate, HibernateUtil.getSession(), returning null

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to get a Session like this:
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:I think you need to get a Session like this:



And what shoul i return on getsession? :-/
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro

What exactly happens when you run the code? What exception (if any) do you receive?
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.






 
Gopakumar Naryanan
Ranch Hand
Posts: 72
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic