|
From: Rob B. <rob...@ve...> - 2003-08-08 13:11:23
|
Timo,
You may run into problems with that solution. Depending upon the precision of how the data is stored in your database, you may have a wide window where multiple objects have the same date.
Let's just say, for sake of arguement, that your database only holds date to the nearest second, and loses any sub-second accuracy. If two objects are created within the same second in your app, and then stored to the DB, when they are pulled back they will have the same created time, and thus your code will think they are the same object.
If you want a better solution for this scheme of generating a hashcode, use a GUID / UUID generator. These take time / machine (usually IP address in Java, network card MAC address in C.) and usually other "unique/random" parameters into account for creating an ID. Using a good GUID / UUID generator with a good algorithm will GUARANTEE that no two id's are the same, without the need for any synchronization between machines (like hi/low, sequence, etc require). Since Java based GUID / UUID generators generally use the machines IP address as part of the algorithm, you need to be sure that the IP address of each machine is unique. This is usually not a problem in a lan / lan cluster environment. Also make sure your algorithm doesn't mistakenly use 127.0.0.1 as it's IP address, as all machines have that IP.
Hope this helps out.
Later
Rob
>
>
> Hi all!
>
> I think I finally have a solution to the hashCode/equals problems!
>
> In every table I have a field/property named "created" of type datetime,
> which may not be null. My domain objects hence all have a "created"
> property, which is initialized with the current time on instantiation
> ("new java.util.Date()"). Other applications using the database
> directly MUST supply values for "created" (be they bogus or not).
> "created" may not be altered once set, which could even be enforced
> with the use of triggers/permissions on the DB side and a private
> setCreated() method on the Java side.
>
> Now I "calculate" hashCode() based on the "created" property (invoke
> getCreated().hashCode()) and equals() still based on ID.
>
> This should solve all problems issued before!
>
> Opinions? Have I missed anything?
>
>
> Regards,
>
> Timo
|