google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Incorrect result when compound key used #297

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
MongoDB 1.6.5, Morphia 0.99.

I am using compound primary key with Morphia:

public class MyId {
    private long a;
    private long b;
}

@Id
private MyId id;

This seems to work fine up until EntityCache is not involved. MongoDB returns 
BasicDBObject instead of primitive in _id column. This in turns causes the 
following code (com.google.code.morphia.Key#compareTo) to silently swallow 
ClassCastException and return 0 ("equality"):

try {
    cmp = compareNullable((Comparable<?>)this.id,(Comparable<?>)other.id);
    if (cmp != 0)
        return cmp;
} catch (Exception e) {
    //continue
}
return 0;

This has a disastrous effects - two objects having completely different ids are 
considered equal, making Morphia to return incorrect results (false positive 
cache hits).

To make matters worse, there is no obvious workaround. Replacing mapper to 
disable entity caching is not possible since Mapper instance is created in 
Morphia c-tor and can't be altered (which should also be fixed to allow further 
Morphia customizations).

Original issue reported on code.google.com by nurkiew...@gmail.com on 7 Jul 2011 at 12:53

GoogleCodeExporter commented 9 years ago
Please prioritize this bug as this is a show-stopper bug and there is no easy 
work-around.

Original comment by morten.h...@gmail.com on 19 Jul 2011 at 8:18

GoogleCodeExporter commented 9 years ago

Original comment by scotthernandez on 19 Jul 2011 at 8:28

GoogleCodeExporter commented 9 years ago
Attached is a patched version of Key.java that falls back to using 
Object.equals and String.compareTo for non-comparable objects. Let me know if 
it is a good solution as I am currently using it to fix compound key queries.

Original comment by patr...@springmetrics.com on 28 Jul 2011 at 5:21

Attachments:

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1756.

Original comment by scotthernandez on 5 Aug 2011 at 9:48