public abstract class BasicData extends java.lang.Object implements IdentifiableData
getId()
property
and overrides the default implementations of the equals(Object)
and hashCode()
methods.
For all practical purposes object identity is the same as database identity, ie.
two objects are considered equal if they reference the same row in the database.
Objects that has not yet been saved to the database should be handled carefully
and not be put into Set
:s or Map
:s since that may break
the contract for the equals()
and hashCode()
methods.
We recommend that the equals()
and hashCode()
methods
not are overridden in subclasses, unless you really know what you are doing.
Actually, we would have liked to make the methods final
but that
would disable the proxy feature of Hibernate.
Modifier and Type | Field and Description |
---|---|
private int |
hashCode
Stores the hash code of the item.
|
private int |
id |
private int |
version |
Constructor and Description |
---|
BasicData() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other)
Check if this item is equal to another item.
|
int |
getId()
Get the id of the item.
|
int |
getVersion()
Get the version of the item.
|
int |
hashCode()
For new items (getId() returns 0) the hash code
is the same as returned by the
System.identityHashCode() method. |
(package private) void |
setId(int id)
Set the id of the item.
|
(package private) void |
setVersion(int version)
Set the version of the item.
|
java.lang.String |
toString()
The string will look like
ClassName[id=55] or
ClassName[new] . |
private int hashCode
hashCode()
private int id
private int version
public java.lang.String toString()
ClassName[id=55]
or
ClassName[new]
.toString
in class java.lang.Object
public boolean equals(java.lang.Object other)
other
object can be a Hibernate proxy, which is
a dynamically generated subclass of the actual class.
Do not override this method in a subclass!equals
in class java.lang.Object
public int hashCode()
System.identityHashCode()
method.
Items loaded from the database uses the id
as the hash code. We must also
consider the case were a new object is saved to the database. Our recommendation
is to not place the object in Set
(or other structure) that
calls the hashCode
method before the object is saved to the
database. If you do that the contract for the equals()/hashCode()
method is broken and equals()
may return true even if hashCode()
returns different values.
Do not override this method in a subclass!hashCode
in class java.lang.Object
public int getId()
getId
in interface IdentifiableData
void setId(int id)
id
- The id of the item@NotLoggable public int getVersion()
getVersion
in interface IdentifiableData
void setVersion(int version)
version
- The version of the item