Hi Steve,

> Comparing an Object with a String using toString() seems to me like a misuse
> of toString(). It will solve your particular case, but I don't think it is a
> good general approach.

Well, if like Comparable a marker interface would remove that feel of
misuse, then so be it. But toString is not that bad to me because, as
the Javadoc puts it: "Returns a string representation of the object.".
Someone using a Java object in a database is already an advanced user,
and comparing an object with a string would be logical to compare the
string representation of that object rather than its serialized bytes.
Not implementing the toString method just falls into the category of
errors like not implementing hashCode and equals when using HashMaps.


> In your case, I'd recommend adding an additional database column to the
> table. This column would be for the String representation, with type
> VARCHAR, and would be for the purposes of ordering.

First, this is a workaround. Second, if that workaround could work in
my case, I would do it :)   Unfortunately, my use case is more complex
so I have to modify H2 itself...

I am basically auto converting an existing tabular structure to tables
in H2 with type detections and so on. There has to be a strict mapping
between the fields of my system and the columns of the tables so I
cannot insert additional columns. Simple types worked fine except for
one special type (and there could be more coming...) for which I
started using the Java object approach.

Conceptually, there has to be a way for a user-defined type (Java
Object) to also define how it behaves:
- how to convert it to other types.
- how to compare it, whether against other Java objects or other SQL types.

I think Comparable is a logical choice, and I think comparison to
other types would make sense for the String type, for which the
toString method is in my view a good candidate (cf. its Javadoc, not
its misuse by users).

Cheers,
-Christopher

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to