2.17.2: 2011-06-17

net.sf.basedb.core.data
Class BasicData

java.lang.Object
  extended by net.sf.basedb.core.data.BasicData
All Implemented Interfaces:
IdentifiableData
Direct Known Subclasses:
AnnotationData, AnnotationSetData, AnyToAnyData, ArrayDesignBlockData, ArrayDesignPlateData, BioAssayData, BioAssaySetData, BioMaterialEventData, BioPlateEventParticipantData, BioPlateEventTypeData, BioPlateTypeData, BioWellData, ChangeHistoryData, ChangeHistoryDetailData, ContextData, DataCubeColumnData, DataCubeData, DataCubeExtraValueData, DataCubeFilterData, DataCubeLayerData, DataFileTypeData, DiskUsageData, ExtraValueData, ExtraValueTypeData, FeatureData, FileSetData, FileSetMemberData, FileTypeData, GroupData, HardwareTypeData, HelpData, ImageData, JobAgentSettingsData, KeyData, MessageData, MimeTypeData, NewsData, OwnedData, ParameterValueData, PasswordData, PlateEventData, PlateEventTypeData, PlateGeometryData, PlatformData, PlatformFileTypeData, PlatformVariantData, PluginTypeData, ProtocolTypeData, QuantityData, QuotaData, QuotaTypeData, RawData, ReporterData, ReporterTypeData, RoleData, SchemaVersionData, SessionData, SettingData, SoftwareTypeData, SpotImagesData, TransformationData, UnitData, UnitSymbolData, UserData, VirtualDbData, WellData

public abstract class BasicData
extends Object
implements IdentifiableData

This is the root superclass of all data classes. Each class must inherit from this class or one of the subclasses. The main purpose of this class is to handle object identity. It contains a Hibernate mapping for the 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.

Version:
2.0
Author:
Nicklas
See Also:
Basic classes and interfaces

Field Summary
private  int hashCode
          Stores the hash code of the item.
private  int id
           
private  int version
           
 
Constructor Summary
BasicData()
           
 
Method Summary
 boolean equals(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.
 String toString()
          The string will look like ClassName[id=55] or ClassName[new].
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

hashCode

private int hashCode
Stores the hash code of the item. This value is initialised the first time it is asked for by an application.

See Also:
hashCode()

id

private int id

version

private int version
Constructor Detail

BasicData

public BasicData()
Method Detail

toString

public String toString()
The string will look like ClassName[id=55] or ClassName[new].

Overrides:
toString in class Object

equals

public boolean equals(Object other)
Check if this item is equal to another item. They are considered to be equal if they are of the same class and have the same ID. Items that has not yet been assigned an ID (id == 0) are equal only if they refer to the same object instance. We must also handle the fact that the 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!

Overrides:
equals in class Object

hashCode

public int hashCode()
For new items (getId() returns 0) the hash code is the same as returned by the 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!

Overrides:
hashCode in class Object

getId

public int getId()
Get the id of the item. The id is automatically generated by the database the first time the item is saved to the database. A new item has the value 0.

Specified by:
getId in interface IdentifiableData
Returns:
The id of the item or 0
Hibernate: id
column="`id`" type="int" generator-class="native" unsaved-value="0"

setId

void setId(int id)
Set the id of the item. Only used by Hibernate.

Parameters:
id - The id of the item

getVersion

public int getVersion()
Get the version of the item. This value is used by Hibernate to check if another process or thread has modified the corresponding row in the databse since this item was loaded from the. If that is the case, this item can't be saved and an exception will be thrown.

Specified by:
getVersion in interface IdentifiableData
Hibernate: version
column="`version`" type="int"

setVersion

void setVersion(int version)
Set the version of the item. Only used by Hibernate.

Parameters:
version - The version of the item

2.17.2: 2011-06-17