• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate entity a.equals(b) but a != b

 
Juan Rolando Prieur-Reza
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does hibernate allow the more than one entity node
to represent the same corresponding record in the database (in the same JVM application instance)?

I suspect it would not allow that because it would allow a number of inconsistent and unequal nodes to exist.
If updated, which one would be persisted?

Consequently, to test whether node A represents the same instance of an entity B,
you should be able to say if (A == B). Or can you?

Often, for many types of classes such as String, we say if (A.equals(B))
since we allow more than one instance be considered equal because they contain the same data.
That occurs naturally when A and B are created from different sources and algorithms for different purposes. We don't rely on the existing String to be found and referenced instead of a duplicate created.
E.g. node A defines a keyword such as "table", and node B is created from a reading a file of config information, say.

So, what situation, if any, can cause hibernate's entity nodes to be A.equals(B) but at the same time A != B ?
[ July 30, 2008: Message edited by: juan prieur ]
 
Juan Rolando Prieur-Reza
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in the Hibernate Reference Documentation Version: 3.2.2

11.1.3. Considering object identity
An application may concurrently access the same persistent state in two different Sessions. However, an instance of a persistent class is never shared between two Session instances. Hence there are two different notions
of identity:
Database Identity
foo.getId().equals( bar.getId() )
JVM Identity
foo==bar

... Within a Session the application may safely use == to compare objects.
[ July 30, 2008: Message edited by: juan prieur ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic