-->
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.  [ 6 posts ] 
Author Message
 Post subject: 2.1.4 strange behaviour of a set.
PostPosted: Wed Jun 02, 2004 2:57 pm 
Newbie

Joined: Mon May 31, 2004 10:15 am
Posts: 4
Location: Argentina
I am experiencing some behaviour that to me looks to be a bug, but I just need quick affirmation to know whether I should post the code and mapping files.

I have an exact replica of the Parent/Child example in my code. I am in the *Parent* class attempting to to ascertain if the Parent contains the Child (Here TimeSheetItem is the Child, and TimeSheet is the Parent), before I try to delete it. I cant remember if the Parent/Child example is lazy load, but mine is, so keep that in mind.

Under any circumstances, (barring concurrent modifications; there is only one thread accessing this method, it cannot have anything to do with concurrency (from the perspective of my code at least)) Could the two print statements within the for loop print true, and the print statement which checks if the element is contained print false. This code is in the same method, and nothing is called, in between nor, are any other threads running. It would seem to me that the contains method might have a bug, I have overidden equals in my classes, but it would seem that by virtue of printing true above, the contains method should certainly return true, no _ ?

Code:
for (Iterator ir = getTimeSheetItems().iterator(); ir.hasNext(); ) {
   TimeSheetItem i = (TimeSheetItem) ir.next();

   System.out.println(tsi.equals(i));
   System.out.println(i.equals(tsi));
}
   
System.out.println(getTimeSheetItems().contains(tsi));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:02 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:25 pm 
Newbie

Joined: Mon May 31, 2004 10:15 am
Posts: 4
Location: Argentina
Okay, based on your response - I must not have been entirely clear what I was asking, or the situation I am in.

Code:
public boolean deleteTimeSheetItem(TimeSheetItem tsi) 
{
boolean retval;
      
for (Iterator ir = getTimeSheetItems().iterator(); ir.hasNext(); ) {
  TimeSheetItem i = (TimeSheetItem) ir.next();
  System.out.println(tsi.equals(i));
  System.out.println(i.equals(tsi));
}
      
try {
  if (getTimeSheetItems().contains(tsi)) {
   getTimeSheetItems().remove(tsi);
   HibernateUtil.delete(tsi);
   retval = true;
}
else {
   retval = false;
}
}
catch (Exception he) {
log.error("Unable to delete TimeSheetItem.", he);
   retval = false;
}
return retval;
}


This method is printing true twice above when it encounters the element that is *in* the list, however it is never returning true, from the call to contains. the getTimeSheetItems method is a simple return of the timeSheetItems Set. Here are the two mapping chunks (just the set parts)

In TimeSheet.hbm.xml
Code:
<set
            name="timeSheetItems"
            lazy="false"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >
        <key
             column="timesheet_id"
         >
         </key>

              <one-to-many
                  class="com.sony.tsr.entities.TimeSheetItem"
              />
</set>


In TimeSheetItem.hbm.xml
Code:
<many-to-one
            name="timeSheet"
            class="com.sony.tsr.entities.TimeSheet"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="timesheet_id"
            not-null="true"
        />


I hope this is clearer, maybe from the nature of your response, what I am witnessing is absurd, but I have stared at it for a while.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:49 pm 
Newbie

Joined: Mon May 31, 2004 10:15 am
Posts: 4
Location: Argentina
In looking for the problem I have variously tried different things; If I call:

Code:
try {
   HibernateUtil.currentSession().refresh(this);
} catch (HibernateException e) {
   e.printStackTrace();
}


In the method deleteTimeSheetItem(..) before the call to contains then it correctly deletes the TimeSheetItem. According to the semantics of Set, it would seem that by virtue of printing true from within the for loop, it should also return true from the contains call, but unless I call refresh before the call to contains, it does not return true. Is my logic flawed ?

jim


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 12, 2004 1:43 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Don't know if you're still tracking this thread but I ran into the same exact problem.

I suspect your hashcode for the objects within the set is being modified in some way after the object is added.

The HashSet will key off of the hashcode of the object at the moment it is added to the Set. If you change the hashcode after this fact you will break the contains and remove.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 30, 2004 10:21 am 
Newbie

Joined: Fri Aug 27, 2004 8:27 am
Posts: 4
Hi.
I'm using Hibernate 2.1.4 and i have trouble with Set.
This is method, whitch calculates total price of an order:
Code:
    public double getTotal(){
        double total=0;
        Collection orderLines = this.getOrderLines();
        Iterator iter = orderLines.iterator();
        while (iter.hasNext()){
            double lineTotal = ((OrderLine)iter.next()).getTotal();
            total+=lineTotal;
        }
        return total;
    }

Some orders have several order lines, but orderLines.size() returns 1. Iterator finds only one element and this method returns price of one element only.
Here is mapping for Order object:
Code:
<hibernate-mapping>
    <class name="com.exshop.Order" table="exshop.ORDERS">

        <id name="id" type="integer" unsaved-value="0" >
            <column name="ID" sql-type="INTEGER" not-null="true"/>
            <generator class="increment"/>
        </id>

        <property name="date">
            <column name="Date" sql-type="DATE" not-null="true"/>
        </property>

        <many-to-one name="user" class="com.exshop.User" column="UserName" unique="true"/>

        <set name="orderLines" lazy="false">
            <key column="ORDER_ID"/>
            <one-to-many class="com.exshop.OrderLine"/>
        </set>

    </class>
</hibernate-mapping>

I have a queston:"Where are other order lines?". I am just a "new user", so, please, tell me what's wrong.
Thank you.


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