1

My team is using Hibernate Tools 4.0.0 on the current project, and I am using FindBugs. It's flagging that the generated ORM code's equals method is using == instead of equals.

I searched but couldn't find how to change that. From my understanding, shouldn't equals be used instead of ==? If so, how to do it (via hibernate.reveng.xml or something else)?

Update: I needed to scroll right to see the rest of the line of code generated. Turns out, the first part that was flagged is doing a memory reference. However, it 's OR'd with a comparison using equals. So this is simply a case of needing to read more carefully.

user1766760
  • 631
  • 2
  • 8
  • 26

1 Answers1

1

it depends on what is being compared. == compares refrences when dealing with objects, while .equals() compares value of the objects (if .equals() is supported by those objects).

it's possible that the for the generated code, == is appropriately used. It would depend on the context of course.

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
  • Since this is an id class, more specifically primary keys to a table. I'm assuming it is dealing with object values, not references. It doesn't make a lot of sense to say two things aren't equal if their java references are different. The database doesn't see it that way ;) Plus, I am going with the understanding that this is overriding `Object`'s `equals`, which again deals with values, not references. – user1766760 Oct 04 '13 at 16:57
  • Though I guess another way to look at this is, somehow Hibernate *doesn't* consider objects to be the same unless their references are the same? I don't know why that really bothers me, but I can't seem to find anything online... that usually means my understanding is wrong. It doesn't make any sense. – user1766760 Oct 04 '13 at 17:04
  • Example: `String a = "someString"; String b = a; if (a == b) { System.out.println("Same reference!"); }` prints "Same reference!" since both a and b objects refer to the same memory location (similar to pointer). – SnakeDoc Oct 04 '13 at 17:04
  • `String a = "someString"; String b = "someString"; if (a == b) { System.out.println("Same reference!"); }` prints "Same reference!" also, since the jvm does some magic in the background and cache's the string literal objects. so now you have two references to the same thing in memory. – SnakeDoc Oct 04 '13 at 17:05
  • But -- `String a = "someString"; String b = new String"someString"); if (a == b) { System.out.println("Same reference!"); }` does not print anything, because `new String()` creates a new object in memory and sets b's reference to point at it. – SnakeDoc Oct 04 '13 at 17:09