Stuart-campbell / RushOrm

Object-relational mapping for Android
http://www.rushorm.co.uk/
Other
172 stars 19 forks source link

Overriding hashCode() leads to objects with empty children #94

Open Cy4n1d3 opened 8 years ago

Cy4n1d3 commented 8 years ago

I'm starting a new ticket for this as the other one is closed already.

Closely related to #78 I guess.

I'm kinda in the same spot right now as olavbg has been when he opened up this (#78) ticket.

I am overwriting equals() and hashCode() as I need to compare Lists and use contains() while disregarding any FilterValues which may have been (user)set before.

I have the following (pseudocode) classes:

class Filter extends RushObject {
    private int filterId;
    String name;
    String type;
    List<FilterValue> filterValues;

    // a bunch of constructors, getters, setters, methods

    @Override
    public int hashCode() {
        int hash = 17;
        hash = 31 * hash + filterId;
        hash = 31 * hash + (name != null ? name.hashCode() : 0);
        hash = 31 * hash + (type != null ? type.hashCode() : 0);

        return hash;
    }
}

class FilterValue extends RushObject {
    private int filterValueId;
    private String value;

    // snipped
}

On first start of the application (fresh database) while modifying and saving a Filter in my FilterFragment this Filter is being duplicated afterwards (without child objects - List is being omitted in this case). This only happens on first start, while staying in the first (Home)View without switching activities and while overwriting hashCode. If I comment out my custom hashCode(), this object duplication does not occur.

Possibly clarifying output from logcat - relevant parts being the Filter-object and the Rush='xxx' property: hashCode() commented out, expected behaviour:

12-12 13:59:01.532    9579-9579/pg.recsys.app.debug W/FilterListAdapter﹕ Saving Filter (row toggled) Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='false', val='false'}]}
12-12 13:59:01.544    9579-9579/pg.recsys.app.debug W/Filter﹕ RUSH Saving: Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='true', val='false'}]}

custom hashCode() in place, unexpected behaviour:

12-12 14:02:47.633  11233-11233/pg.recsys.app.debug W/FilterListAdapter﹕ Saving Filter (row toggled) Filter{fId=12, Rush='false', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='false', val='false'}]}
12-12 14:02:47.642  11233-11233/pg.recsys.app.debug W/Filter﹕ RUSH Saving: Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='true', val='false'}]}

Explanation of log output:

I could of course work around this problem by writing a custom contains() method which does not use equals() (meaning me not having to overwrite hashCode) but still I'd like to hear if such behaviour is to be expected? If yes, what's the reason for this?

PS: the same problem also applies to the FilterValue class which I've written a custom equals() and hashCode() for. As I don't need those right now I've commented them out for debugging purposes. If I use a custom hashCode on FilterValue as well, these objects also report Rush='false' on being modified after first start.

Kind regards, Christian

Cy4n1d3 commented 8 years ago

Any ideas? :)