Question

I am trying to map a UUID field using Dozer, and running into a problem mentioned on dozer github:

https://github.com/DozerMapper/dozer/issues/83

the problem is - apparently, Dozer relies on a default no-parameter constructor, which is not present in UUID. So, the link shows how to tell Dozer to perform reference copy instead. However, using that hint doesn't help, I am still getting this exception:

org.dozer.MappingException: java.lang.NoSuchMethodException: java.util.UUID.()

The test failing ins this:

@Test
public void testUUIDMapping() {
    UUID source = fakeUUID1;

    UUID result = mapper.map(source, UUID.class);

    Assert.assertEquals(result, source);
}

Finally, the mappings is this, as per the hint in the issues:

<configuration>
    <copy-by-references>
        <copy-by-reference>java.util.UUID</copy-by-reference>
    </copy-by-references>
</configuration>

Any ideas on what I might be doing wrong? Thanks!

Was it helpful?

Solution

You're using a mapper to map from a UUID to another UUID? Shouldn't you be testing that you can map a UUID from class A to class B? When you map the UUID directly, dozer is going to reflectively inspect the fields inside the UUID and try to copy them from one to another.

In other words, I don't think this test is valid. This test passed for me (it needed the configuration you added):

@Test
public void whenMappingAUuidThenItGetsCopiedByReference() {
    ClassWithUuid a = new ClassWithUuid();
    UUID uuid = UUID.randomUUID();
    a.setUuid(uuid);

    Mapper mapper = new Mapper();
    ClassWithUuid b = //map to b
    assertEquals(uuid, b.getUuid());
}

public static class ClassWithUuid {
    private UUID uuid;

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top