Nomin automap causes infinite loop

后端 未结 1 1365
离开以前
离开以前 2021-01-22 05:02

I am using Nomin for mapping tasks. As taken from the documentation of Nomin it should be able to map fields with the same name by itself in case automapping has been activated.

1条回答
  •  心在旅途
    2021-01-22 05:34

    One thing worth adding regarding your use case - this Recursive mapping rule a = b causes infinite loop! exception is thrown because you use groovy classes in your mapping rule. Nomin uses ReflectionIntrospector and what's important:

    It performs getting/setting properties using accessor methods which are called through the Java reflection mechanism. ReflectionIntrospector uses supplied NamingPolicy instance to determine accessor methods. JbNamingPolicy is used by default, this implementation cerresponds the JavaBeans convention. Its InstanceCreator named ReflectionInstanceCreator instantiates objects using Class.newInstance().

    Source: http://nomin.sourceforge.net/introspectors.html

    A simple Groovy class like:

    class Entity {
        String name
        String somethingElse
    }
    

    gets compiled to a Java class that implements GroovyObject providing following methods:

    public interface GroovyObject {
        Object invokeMethod(String var1, Object var2);
    
        Object getProperty(String var1);
    
        void setProperty(String var1, Object var2);
    
        MetaClass getMetaClass();
    
        void setMetaClass(MetaClass var1);
    }
    

    In this case ReflectionInstanceCreator combined with automap() resolves following mappings:

    a.property = b.property
    

    and

    a = b
    

    where a = b mapping comes from MetaClass getMetaClass() getter method I suppose, because there is no mapping like a.metaClass = b.metaClass resolved. a.property = b.property gets resolved because of Object getProperty(String var1) method.

    Solution

    This problem can be solved by specifying explicitly ExplodingIntrospector for your mapping script that:

    It performs getting/setting properties using a class field immediately through through the Java reflection mechanism and may be useful in case when domain object don't provide accessors for their properties. Supplied instance creator is ReflectionInstanceCreator.

    Source: http://nomin.sourceforge.net/introspectors.html

    All you have to do is to add

    introspector exploding
    

    right below mappingFor a: ..., b: ... header. For example:

    import mypackage.Entity
    import mypackage.EntityDto
    
    mappingFor a: Entity,  b: EntityDto
    introspector exploding
    automap()
    
    a.test2 = b.test1
    

    Tested with two Groovy classes, worked like a charm. Hope it helps.

    0 讨论(0)
提交回复
热议问题