-->
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: problem with Sets
PostPosted: Wed Nov 30, 2005 7:50 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
Hi Everyone,

I'm having a problem and really need help...

Schema

Employee:
Code:
    <class name="com.test.Employee" table="employee_table">
        <id name="id" type="java.lang.Integer">
           <column name="fid"/>
            <generator class="assigned" />
        </id>
        <property name="id2" column="fid2" type="java.lang.Integer"/>
        <many-to-one name="info" column="fid2" class="com.test.EmployeeInfo" insert="false" update="false"/>
        // ...
    </class>


EmployeeInfo:
Code:
    <class name="com.test.EmployeeInfo" table="employee_info_table">
        <id name="infoId" column="fid2" type="java.lang.Integer">
            <generator class="assigned" />
        </id>
        <set name="educations" lazy="false">
           <key column="fid2" />
           <one-to-many class="com.test.Education" />
        </set>
        // ...
    </class>


Education:
Code:
    <class name="com.test.Education" table="education_table">
       <composite-id>
         <key-property name="infoId" column="fid2" length="6" type="java.lang.Integer"/>
         <key-property name="educationSequenceNumber" column="seq" length="4" type="java.lang.Short"/>
       </composite-id>
        <set name="specialities" lazy="false">
           <key>
              <column name="fid2"/>
              <column name="edsq"/>
           </key>
           <one-to-many class="com.test.Speciality" />
        </set>
        // ...
    </class>


Speciality:

Code:
    <class name="com.test.Speciality" table="speciality_table">
       <composite-id>
         <key-property name="infoId" column="fid2" length="6" type="java.lang.Integer"/>
         <key-property name="educationSequenceNumber" column="edsq" length="4" type="java.lang.Short"/>
         <key-property name="specialitySequenceNumber" column="seq" length="4" type="java.lang.Short"/>
       </composite-id>
        // ...
    </class>


I do:
Code:
   Employee employee = (Employee) hSession.load(Employee.class, new Integer(30));
   // employee has info, info has educations, some educations have specialities
   for (Iterator iter = employee.getInfo().getEducations().iterator(); iter.hasNext(); ){
      Education educ = (Education) iter.next();
      Set spec = educ.getSpecialities();
      system.out.println(spec.isEmpty());
      // works fine for educations that have specialities
      // but fails (exceptions) on education that don't have specialities (see below)
   }


Exception:
Code:
java.lang.NullPointerException
   at net.sf.hibernate.collection.Set.isEmpty(Set.java:115)
   at org.apache.jsp._index._jspService(_index.java:174)
   at com.ibm.ws.webcontainer.jsp.runtime.HttpJspBase.service(HttpJspBase.java:89)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at com.ibm.ws.webcontainer.jsp.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:344)
   at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.serviceJspFile(JspServlet.java:683)
   at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.service(JspServlet.java:781)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
   at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
   at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
   at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
   at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
   at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:76)
...


similar exception if I try toString(), size(), etc. on that Set...

Thanks

_________________
--
Pavel


Top
 Profile  
 
 Post subject: as if...
PostPosted: Thu Dec 01, 2005 12:00 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
It looks as if the Set is not being properly initialized when the ResutSet is empty...

_________________
--
Pavel


Top
 Profile  
 
 Post subject: Re: as if...
PostPosted: Thu Dec 01, 2005 12:41 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:34 am
Posts: 28
pjwerwer wrote:
It looks as if the Set is not being properly initialized when the ResutSet is empty...


Quite possibly. I think what you should do is...

for (Iterator iter = employee.getInfo().getEducations().iterator(); iter.hasNext(); ){
Education educ = (Education) iter.next();
Set spec = educ.getSpecialities();

if(spec != null)
{
system.out.println(spec.isEmpty());
// Any other operations you may want to do
}
}


That or replace the if statement w/ a try/catch


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you can reproduce this in a simple JUnit test case (NOT in a scriplet in a JSP!), submit it to JIRA and we will take a quick look.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 12:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
gavin wrote:
If you can reproduce this in a simple JUnit test case (NOT in a scriplet in a JSP!), submit it to JIRA and we will take a quick look.


I should say, if you can reproduce it in HB 3.1, latest rc.


Top
 Profile  
 
 Post subject: Re: as if...
PostPosted: Thu Dec 01, 2005 12:48 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
mineame wrote:
if(spec != null)
{
system.out.println(spec.isEmpty());
// Any other operations you may want to do
}


spec is always NOT null. There is something null inside the net.sf.hibernate.collection.Set

If only it was that easy... :(

_________________
--
Pavel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 12:54 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
gavin wrote:
gavin wrote:
If you can reproduce this in a simple JUnit test case (NOT in a scriplet in a JSP!), submit it to JIRA and we will take a quick look.


I should say, if you can reproduce it in HB 3.1, latest rc.


I'll try to make a TestCase, but it will be for 2.1.8... I'm kind off forced to use it... hope it's OK...

Are there any requirements (formal document, url?) for test cases that are being submitted to JIRA?

Thanks.

_________________
--
Pavel


Top
 Profile  
 
 Post subject: Re: as if...
PostPosted: Thu Dec 01, 2005 12:54 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:34 am
Posts: 28
pjwerwer wrote:
mineame wrote:
if(spec != null)
{
system.out.println(spec.isEmpty());
// Any other operations you may want to do
}


spec is always NOT null. There is something null inside the net.sf.hibernate.collection.Set

If only it was that easy... :(


I understand that. Regardless, you probably should still be testing for null w/ objects that are being returned from other libraries that you have no contract (i.e. garauntees that null will not be returned) with.

My suggestion is just good defensive programming that will help you along until the problem is fixed. Not a be all end all.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you submit something for 2.x, I won't have time to look at it.


Top
 Profile  
 
 Post subject: Re: as if...
PostPosted: Thu Dec 01, 2005 1:01 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
mineame wrote:
I understand that. Regardless, you probably should still be testing for null w/ objects that are being returned from other libraries that you have no contract (i.e. garauntees that null will not be returned) with.

My suggestion is just good defensive programming that will help you along until the problem is fixed. Not a be all end all.


Agreed. But I though that hibernate guarantees that Sets will not be null, but will be of size=0... isn't it?

You gotta stop somewhere and believe that documentation and the underlying libraries are fine... or you'll end up doing stuff like:

Code:
Object test = new Object();
if (test != null) {
   // use test
}

_________________
--
Pavel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 3:02 pm 
Newbie

Joined: Wed Nov 30, 2005 7:12 pm
Posts: 6
Location: Edmonton / Canada
Ok, I think I found the problem but can't explain it...

It seem like there is something wrong with my hashCode() method in Education...

I had:
Code:
    public int hashCode() {
        return new HashCodeBuilder(11, 17)
           .append(this.infoId)
           .append(this.educationSequenceNumber)
           .append(this.educationInfo)
           .append(this.completionDate)
           // include ALL other fields (except specialities)
           .toHashCode();
    }


but when I left just the keys:
Code:
    public int hashCode() {
        return new HashCodeBuilder(11, 17)
           .append(this.infoId)
           .append(this.educationSequenceNumber)
           .toHashCode();
    }

it started working like a charm...

Ideas?

Thanks.

_________________
--
Pavel


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.