-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Getting a NullPointerException when doing query
PostPosted: Wed Feb 08, 2006 11:36 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
The Publisher table has only one row. When I do a get everything is fine. I can insert and update. I just can't do a query. I am getting this problem with other tables as well. I picked the simplest table to show the problem.
I am using Hybernate Syncronizer to generate the framework.

Hibernate version:3.1.2

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="name.elliot.bill.pom">
   <class
      name="Publisher"
      table="publisher"
   >
      <id
         name="id"
         type="java.lang.Integer"
         column="publisherID"
      >
         <generator class="native"/>
      </id>

      <property
         name="name"
         column="name"
         type="java.lang.String"
         not-null="true"
         length="255"
      />

   </class>
   
   <query name="library.publisher.byName"><![CDATA[
      from Publisher
      where name = 'JavaX'
   ]]></query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
        PublisherDAO daoP = new PublisherDAO();
      Integer id;
        List results;
        Publisher publisher;
        Query query;

        publisher = daoP.get( new Integer(4) );
        if( logger.isInfoEnabled() ) {
           if( publisher != null ) {
              logger.info("Publisher name = " + publisher.getName() );
           } else {
              logger.info("Publisher was not found");
           }
        }
        query = daoP.getQuery("from Publisher");
        assertNotNull(query);
        results = query.list();


Full stack trace of any exception that occurs:
Code:
Exception in thread "main" java.lang.NullPointerException
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:442)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:368)
   at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561)
   at org.hibernate.loader.Loader.doQuery(Loader.java:661)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2150)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
   at org.hibernate.loader.Loader.list(Loader.java:2024)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:300)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:146)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1093)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
   at name.elliot.bill.EntryTest.testEntryDAO(EntryTest.java:62)
   at name.elliot.bill.EntryTest.main(EntryTest.java:35)


Name and version of the database you are using:
MySQL 4.1 with JConnect version 3.1.12

The generated SQL (show_sql=true):
Code:
20:28:17,004  INFO EntryTest:40 - Entered testEntryDAO
ibernate:
    select
        publisher0_.publisherID as publishe1_1_0_,
        publisher0_.name as name1_0_
    from
        publisher publisher0_
    where
        publisher0_.publisherID=?
20:28:22,833  INFO EntryTest:55 - Publisher name = JavaX
Hibernate:
    select
        publisher0_.publisherID as publishe1_1_,
        publisher0_.name as name1_
    from
        publisher publisher0_


Debug level Hibernate log excerpt:
I was not able to get hibernate to output log info. I have log4j jar in class path as well as log4j.properties. But, all I can get to output is my own logging lines. None from hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 11:54 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Code:
  query = daoP.getQuery("from Publisher");


What does the impementation of that function do ?

Code:
Query getQuery(String q) {
return Session.createQuery(q);
}


?? or are you calling Session.getNamedQuery() ?

Just a thought.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 9:53 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
It just does a session.createQuery. I've also tried using named queries as well with the same result.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 11, 2006 10:32 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
change "from Publisher" to "from Publisher p" I don't think an alias is mandatory for that simple case, but in my code I do it anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 11, 2006 4:01 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
Thank you. But, I've already tried various variations of that already :-)

I've tried from Publisher pub, and from Publisher as pub.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 11, 2006 5:23 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
I didn't think 3.1.2 supports the "AS" keyword in hql now.

Okay have you tried using a different JDBC driver, is JConnect your JDBC driver, what about the mysql-connector-3.1.2-bin.jar ?

I've not heard of JConnect before but the top google links indicate a Sybase JDBC driver. Its not clear if you are able to get any information from the MySQL database with that driver or if this is the first access.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 5:31 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
Sorry JConnect was the old name for mysql-connector (I think :)). Yes, I've tried mysql-connector-java-3.0.17-ga-bin.jar and mysql-connector-java-3.1.12-bin.jar.

I also tried running this code with debug enabled and put break points in all the publisher.class menthods. I get a stop in the no args constructor and the setName (it is passed a value of null). The setId method is never called. It is after the setName is called that the exception is thrown.

Do you you have any suggestions on how to get the log4j output from hibernate to be displayed? Or what might be preventing it? I get my own log statements and I have the example log4j.properties in my class path. In it I have turned everything to debug.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 11:25 am 
Newbie

Joined: Thu Apr 14, 2005 9:38 am
Posts: 17
Hey,

I'm having this problem now, Has anyone found a solution to this?

Thank you,


Steve.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 10:06 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
The best I have found is that the tool I was using RAD V6.0 was some how suppressing the log4j output from Hibernate. I imported my project to a raw Eclipse and the log4j output started to appear. Unfortunately, it did not enlighten me to the problem. There were no errors or warnings before the NullPointerException occured.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 2:47 pm 
Newbie

Joined: Thu Apr 14, 2005 9:38 am
Posts: 17
Hey,

I think I may have figured it out. It took me a while but I think I got it. It may not be the solution to your problem though.

Basically, I was running the hibernate code inside a JMS bean. in the onCreate of the JMS bean, I initialized the hibernate factory and hibernate session. Basically so I can re-use the session over and over for each Message(for effitiency). Then I thought that somehow the hibernate session was loosing the session when the first message was done processing.

I then moved the initialization of the hibernate session from the onCreate to the onMessage and it works every time now.

Thank you,


Steve.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 11:02 pm 
Newbie

Joined: Wed Feb 08, 2006 11:13 pm
Posts: 6
Location: Calgary, AB
Thank you Steve.

That was my problem as well. The Hibernate Synconizer tool was getting in the way and had a bug in the DAO code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.