Hi Christopher,
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.
You do make a good point that if a Java Object that implements Comparable
is stored in a column, then one should expect that ordering would use the
Object's compareTo(...) method.
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.
Regards,
Steve
On Saturday, 16 June 2012 15:31:31 UTC+2, Christopher Deckers wrote:
>
> Hi,
>
> We are using H2 (h2-2012-05-23) and we faced a few issues.
>
> One of them is that I replaced a VARCHAR field with a Java object (OTHER)
> that contains the string and some additional information.
> The problem now is that it breaks existing ORDER BY clauses and equality
> tests with VARCHARs, though it implements the CompareTo method (for objects
> of same type) and the toString() method.
>
> I think that comparing the serialized bytes is nonsense for the Java
> object case, and I think that a Java Object's toString and compareTo method
> should be used when appropriate.
>
> As a temporary(?) solution, I changed the following code:
>
> * In "Value.getHigherOrder(int t1, int t2)", line 325, I added:
>
> if(t1 == Value.STRING && t2 == Value.JAVA_OBJECT || t2 ==
> Value.STRING && t1 == Value.JAVA_OBJECT) {
> return Value.STRING;
> }
>
>
> * In "ValueJavaObject", I added these:
>
> @Override
> protected int compareSecure(Value v, CompareMode mode) {
> if(v.getType() == Value.STRING) {
> Object o = Utils.deserialize(getBytesNoCopy());
> return o.toString().compareTo(v.getString());
> }
> if(v.getType() == Value.JAVA_OBJECT) {
> Object o = Utils.deserialize(getBytesNoCopy());
> if(o instanceof Comparable) {
> Object o2 = Utils.deserialize(v.getBytesNoCopy());
> if(o.getClass() == o2.getClass()) {
> return ((Comparable)o).compareTo(o2);
> }
> }
> }
> return super.compareSecure(v, mode);
> }
>
> @Override
> public String getString() {
> return Utils.deserialize(getBytesNoCopy()).toString();
> }
>
>
> Note: I think JAVA_OBJECT should take low order only for String case which
> is why I did not try to change "Value.getOrder(int type)".
>
> What do you think of my solution, can there be any problems with what I
> changed?
> Is there something that could be done in H2 itself to solve my issue?
>
> Cheers,
> -Christopher
>
>
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/h2-database/-/_Ymug-bcdc4J.
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.