#4 No way to reference self (this) in field mapping

closed
None
5
2014-08-14
2005-08-31
Anonymous
No

Using field-deep mapping, it is possible to map against
nested elements N levels deep, but there is no way to
map where N == 0 (self, or this).

Example:

I have the following Class defs:

Class Account {
String name;
Address address;
}

Class Address {
String street;
String suburb;
int postcode;
}

Class SimpleAccount {
String name;
String streetName;
String suburb;
int postcode;
}

And the following mapping entries in my dozer xml file:

<mapping wildcard="false">
<classA>SimpleAccount</classA>
<classB>Account</classB>
<field>
<A>name</A>
<B>name</B>
</field>
</mapping>

<mapping wildcard="false">
<classA>SimpleAccount</classA>
<classB>Address</classB>
<field>
<A>streetName</A>
<B>street</B>
</field>
<field>
<A>suburb</A>
<B>suburb</B>
</field>
<field>
<A>postcode</A>
<B>postcode</B>
</field>
</mapping>

Now to map SimpleAccount instance to an Account
with an Address I must do something like:

Account account = new Account();
Mapper.map(simpleAccount, account);

Address address = new Address();
Mapper.map(simpleAccount, address);

account.setAddress(address);

It would be nice if there was some way to specify a
mapping in SimpleAccount to Account mapping
SimpleAccount to the address property, so that we only
had to call

Mapper.map(simpleAccount, account);

I was able to do a quick fix for my own purposes in
version 1.4.6 by adding the following field mapping:
<!-- new field -->
<field>
<A></A>
<B>address</B>
</field>

And the following changes to FieldMap.java

public Object getSrcFieldValue(Object srcObj) {
try {

//@author: nathanm
//see if source references itself ('this' keyword type
ref). Dozer can't do this by default, so added
//check to handle case
if (isSourceSelfReferencing()) {
return srcObj;
}

return getSourceFieldReadMethod(srcObj.getClass
()).invoke(srcObj, null);
} catch (Exception e) {
throw new MappingException(e);
}
}

/**
* Return true if is self referencing. Is considered self
referencing where no other sources are specified,
* i.e., no source properties or #CDATA in the xml def.
* @param parentSourceClass
* @return
* @author nmcdonald
*/
public boolean isSourceSelfReferencing() {
boolean isSelfRef =
(sourceFieldReadMethod == null)
&&
(sourceField.getName() == null)
&&
(sourceField.getTheGetMethod() == null)
&&
(sourceField.getTheSetMethod() == null);
return isSelfRef;
}

Discussion

  • Franz Garsombke

    Franz Garsombke - 2005-08-31
    • assigned_to: nobody --> fgarsombke
     
  • Ben Sullins

    Ben Sullins - 2005-09-22

    Logged In: YES
    user_id=1232046

    lets make this a feature request, closing this as a bug

     
  • Ben Sullins

    Ben Sullins - 2005-09-22
    • assigned_to: fgarsombke --> nobody
     
  • Franz Garsombke

    Franz Garsombke - 2005-10-15

    Logged In: YES
    user_id=550744

    Good idea. I will try to get this in the next release.

    Thanks.

    Franz

     
  • Franz Garsombke

    Franz Garsombke - 2005-10-15
    • assigned_to: nobody --> fgarsombke
     
  • Matt Tierney

    Matt Tierney - 2005-11-02
    • status: open --> closed
     
  • Matt Tierney

    Matt Tierney - 2005-11-02

    Logged In: YES
    user_id=1236069

    If I understand this request correctly, I believe this issues was
    fixed with the latest 1.5.6 release.

     
  • Franz Garsombke

    Franz Garsombke - 2005-11-02
    • status: closed --> open
     
  • Franz Garsombke

    Franz Garsombke - 2005-11-02

    Logged In: YES
    user_id=550744

    I don't believe this was fixed for 1.5.6. We should be able
    to add it to 1.5.7. I will let you know when a patch is
    available.

    Thanks.

    Franz

     
  • Franz Garsombke

    Franz Garsombke - 2005-11-04

    Logged In: YES
    user_id=550744

    This will be ready in the 1.5.7 release.

     
  • Franz Garsombke

    Franz Garsombke - 2005-11-07
    • status: open --> pending
     
  • Franz Garsombke

    Franz Garsombke - 2005-11-07

    Logged In: YES
    user_id=550744

    Implemented for 1.5.7 - we currently only support this as a
    one-way mapping.

     
  • Franz Garsombke

    Franz Garsombke - 2005-11-16
    • status: pending --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks