Curious instance equality issue with Longs

13 views
Skip to first unread message

Jens

unread,
Jun 28, 2011, 12:41:41 PM6/28/11
to google-we...@googlegroups.com
Hi,

I just fixed a bug in my application and the circumstances under which the bug has occurred (or better has been noticed) are really strange and maybe someone here can give me a hint to understand it.

First of all the bug itself was pretty dump because I have done a businessObject.getId() == this.currentBo.getId() check on client side and both instances have been loaded from the server (GWT RPC). So both instances are different (deserialization) and it should be an equals check (must have been late).

The curious thing is that the instance check has worked for me in my development environment (hosted mode as well as compiled)!! So I thought everything is fine with my code and I deployed to production and then the bug occurs (the instance check always returns false). There is no difference between my development environment and the production environment except the data stored in the database (test data vs. real data).
So I have copied production data to my development database and then the bug also occurs in development environment! I know it sounds pretty silly and I am sure the reason is not the actual data but maybe its part of it.
So I have done some logging and it turns out that the Long instances returned by getId() can be the same although the businessObject instances are different. 

So basically:
1.) businessObject == this.currentBo returns false
2.) businessObject.equals(this.currentBo) returns true
3.) businessObject.getId() == this.currentBo.getId() returns true (this should return false!)

The id itself can only be set via the BusinessObject's constructor, so its immutable. 

Does anyone have an idea how this can happen? As the BusinessObjects instances are created by deserializing the GWT RPC payload I thought that every instance is different. How can it happen that Long instances are the same? I do not have any idea that would help me to understand what has happened.

-- J.



Juan Pablo Gardella

unread,
Jun 29, 2011, 2:14:36 AM6/29/11
to google-we...@googlegroups.com
You must use equals() to compare objects. I have similar issue, but inverse. In production run and in dev mode not.

The problem I think is, for example the next expression:

 anInteger == otherInteger 

If are different objects don't work in Java (becouse compare memory pointers), but when GWT compile translate to Javascript works becouse == in Javascript is behave different.

Conclusion, use equals always to compare objets and you will don't have this kind of bugs

Juan



2011/6/28 Jens <jens.ne...@gmail.com>


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/bAJy30kYUf8J.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages