-->
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.  [ 5 posts ] 
Author Message
 Post subject: Newbie help on Sprg-Hibernate impl-reposted to correct forum
PostPosted: Thu Jan 27, 2005 3:43 pm 
Newbie

Joined: Thu Jan 27, 2005 11:39 am
Posts: 4
I think I posted this on the wrong forum before:

I am using Spring and Hibernate together and appear to be running into a mapping issue when I try to do a simple find. I can run the native sql that is generated by hibernate in a command window and it executes fine. I would appreciate some troubleshooting tips along with any assisstance. I believe that I have all logging turned to debug, but still didn't get much in the way of messages.

Hibernate version: 2.1.7

Mapping documents:

<hibernate-mapping package="com.aholdusa.ltw">
<class name="License" table="license">
<id name="id" unsaved-value="null" column="ID">
<generator class="native" />
</id>
<property name="licenseId" type="string" column="LICENSE_ID" />
<property name="initialStartDate" type="long" column="INITIAL_START_DATE" />
<property name="startDate" type="long" column="START_DATE" />
<property name="endDate" type="long" column="END_DATE" />
<property name="cost" type="float" column="COST" />
<many-to-one name="type" foreign-key="FK_license_type" column="TYPE" class="LicenseType" />
<many-to-one name="contact" foreign-key="FK_license_contact" column="CONTACT" class="Contact" />
<many-to-one name="software" foreign-key="FK_license_software" column="SOFTWARE" class="Software" />
</class>
</hibernate-mapping>



<hibernate-mapping package="com.aholdusa.ltw">
<class name="LicenseType" table="license_type">
<id name="id" unsaved-value="null" column="ID">
<generator class="native" />
</id>
<property name="type" type="string" column="TYPE" />
</class>
</hibernate-mapping>

<hibernate-mapping package="com.aholdusa.ltw">
<class name="Contact" table="contact">
<id name="id" unsaved-value="null" column="ID">
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<list name="phone" table="contact_phone_list" outer-join="false">
<key column="CONTACT" />
<index column="ID" type="integer" />
<element column="PHONE_NUMBER" type="string" />
</list>
</class>
</hibernate-mapping>

<hibernate-mapping package="com.aholdusa.ltw" >
<class name="Vendor" table="vendor" >
<id name="id" unsaved-value="null" column="ID" >
<generator class="native"/>
</id>
<property name="name" type="string" column="NAME" />
<property name="vendorno" type="string" column="VENDOR_NUM" />
<many-to-one name="contact" foreign-key="FK_vendor_contact" column="CONTACT" class="Contact" />
</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close()::

I got this from the Spring handbook

Code:
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
   try
   {
      List result = session.find("from com.aholdusa.ltw.License");
         
      if (result == null)
      {
         throw new LTWException("invalid search result");
      }
      return result;
      }
      catch (HibernateException ex)
      {
         throw SessionFactoryUtils.convertHibernateAccessException(ex);
      }



Full stack trace of any exception that occurs:

[1/27/05 11:23:18:513 EST] 786a7a4e JDBCException W net.sf.hibernate.util.JDBCExceptionReporter SQL Error: 0, SQLState: null
[1/27/05 11:23:18:523 EST] 786a7a4e JDBCException E net.sf.hibernate.util.JDBCExceptionReporter Parameter with index of 1 is not set.
[1/27/05 11:23:18:543 EST] 786a7a4e JDBCException W net.sf.hibernate.util.JDBCExceptionReporter SQL Error: 0, SQLState: null
[1/27/05 11:23:18:553 EST] 786a7a4e JDBCException E net.sf.hibernate.util.JDBCExceptionReporter Parameter with index of 1 is not set.


Name and version of the database you are using:

MySql 4.1

The generated SQL (show_sql=true):

Hibernate: select license0_.ID as ID, license0_.LICENSE_ID as LICENSE_ID, license0_.INITIAL_START_DATE as INITIAL_3_, license0_.START_DATE as START_DATE, license0_.END_DATE as END_DATE, license0_.COST as COST, license0_.TYPE as TYPE, license0_.CONTACT as CONTACT, license0_.SOFTWARE as SOFTWARE from license license0_

Debug level Hibernate log excerpt: ?????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 3:54 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I'm not sure what the Spring handbook is, but you should be using org.springframework.orm.hibernate.support.HibernateDaoSupport when talking to Hibernate. Don't muck with the factory separately.

I'm not sure what your problem is exactly, since that expcetion is weird, but I think you have configured some stuff wrong that I would have not done.

For example, unsaved-value="null" should probably be changed to unsaved-value="0", which is more sensible.

Code:
List result = session.find("from com.aholdusa.ltw.License");
         
      if (result == null)
...


This list will never be null, at least not with HibernateDaoSupport. Hibernate directly probably works the same way since I can't see how a null list would be of any use to anyone since you'd have to check for null constantly throughout the code (anti-pattern). It should just be an empty list. Your conditional will never be executed.

You should actually be putting better messages in your exceptions though and start using the Spring framework-agnostic exceptions.

Just read the Spring documentation on how to set this up.

Also, if you typed "spring hibernate" in google, the 3rd results points you here:

http://www.hibernate.org/110.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 4:13 pm 
Newbie

Joined: Thu Jan 27, 2005 11:39 am
Posts: 4
The link that you pointed me to is where I got the code snippet for using Spring with Aspects to retrieve the Hibernate Session and run the query. I will try the suggestion you gave on switching the nulls to 0.

Once I have the Session is there a test I can run with it to make sure my connection is fine? I have tested the connection outside of Hibernate and I can connect and talk to my database just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 8:50 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Honestly, I stopped setting up projects once I did my first one, so the details of how it's done have already escaped me since I usually load up a small project, delete all the classes in it, setup the IDE a little differently, clean up the configuration files a bit and start a new project.

But the best way to test it quickly is to just make a junit test that loads up the spring application context with a custom dao (extending from HibernateDaoSupport) and try out a simple store of a single object. I find that getting this stuff on a new project takes about 5 or 10 minutes.


Top
 Profile  
 
 Post subject: Resolution
PostPosted: Mon Jan 31, 2005 10:57 am 
Newbie

Joined: Thu Jan 27, 2005 11:39 am
Posts: 4
My problem turned out to be 3 fold:

#1 - I was clueless about trace level logging in jakarta commons logging, once I figured out that Hibernate's statements were not being picked up by the log4j logger, I configured my trace log settings correctly in Websphere's Adminconsole and started getting my trace level statements.

#2 - I used primitives for numbers that I was storing in my classes, after I switched to the objects my instantiation errors went away.

#3 - The MySql driver has a bug in the PreparedStatement cache that was setting the parameter counts in my PreparedStatement objects to numbers in the 100000 range. When I set the PreparedStatement cache size to 0 in my data source the problem went away.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.