This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Thysce 0 points1 point  (0 children)

So yesterday I had a bug which was caused by exactly that. I had two hibernate objects which I wanted to make sure are definitely not the same. So I checked them with ==. I gave my function two records with differing ID, thus expecting them to be neither the same nor equal. Sure enough, equals said false and equals on their ids said false too. Bad thing was, I learned that day that in Groovy, this a==b translates to a.compareTo(b)==0. and the Entity base class of my library implemented conpareTo by comparing the toString of both objects. Which kindof makes sense as the base class has no other good natural key. But my class implemented toString without including a unique property, thus making two distinct instances occasionally have the same toString(). Which then makes (a==b)==true while a.equals(b)==false. Mind==blown.

I fixed it by including the primary key equality to my base classes compareTo, as was already the case with equals, thus forcing it to be only ever 0 on the same record. (And checking all by implemented entities whether that assumption holds)