Search code examples
javarestdozer

Dozer mapping/updating a collection


I am trying to map a A-DTO object to an A-DO object, each having a collection (a List) of T-DTOs, and T-DOs, respectively. I am trying to do it in the context of a REST API. It's a separate question whether it's a right approach - the problem I'm solving is a case of update. Basically, if one of the T-DTOs inside the A-DTO changes, I want that change to be mapped into the corresponding T-DO inside the A-DO.

I found relationship-type="non-cumulative" in Dozer documentation, so that the object inside the collection is updated, if present. But I end up with Dozer inserting a new T-DO into the A-DO's collection!

NOTE: I did implement equals! it is based on the primary key only for now.

Any ideas?

PS: and, if you think this is a bad idea to handle updates to a one-to-many dependent entity, feel free to point that out.. I'm not 100% sure I like that approach, but my REST foo is not very strong.

UPDATE

equals implementation:

@Override
public boolean equals(Object obj) {
    if (obj instanceof MyDOClass) {
        MyDOClass other = (MyDOClass) obj;
        return other.getId().equals(this.getId());
    }
    return false;
}

Solution

  • I just had the same problem and I solved it:

    Dozer uses contains to determine if a member is inside a collection.
    You should implement hashCode so that "contains" will work appropriately.

    You can see this in the following documentation page: http://dozer.sourceforge.net/documentation/collectionandarraymapping.html Under: "Cumulative vs. Non-Cumulative List Mapping (bi-directional)"

    Good luck!