Dozer Bean Mapper Configuration with Collections and set-method attribute - java

i habe a question concerning Dozer Bean Mapping. I have the follwing xml configuration parts (i don´t understand some of this facts):
<mapping>
<class-a>
entity.template.TemplateEntity
</class-a>
<class-b>dto.template.TemplateDto
</class-b>
<field>
<a set-method="setLang" get-method="getLang">lang</a>
<b set-method="setLang" get-method="getLang">lang</b>
<a-hint>entity.template.TemplateLanguageEntity</a-hint>
<b-hint>dto.template.TemplateLanguageDto</b-hint>
</field>
</mapping>
What is the concret meaning of "set-method="setLang" get-method="getLang""?
What does the Dozer Bean Mapper do in this part? There is no other configuration, which describes, how to two collection should be mapped?
<a-hint>entity.template.TemplateLanguageEntity</a-hint>
<b-hint>dto.template.TemplateLanguageDto</b-hint>
Does the Dozer Mapper map all fields automatically, which are founded by them if no configuration was set?
Thanks for helping !
Greetz
Marwief

What is the concret meaning of "set-method="setLang" get-method="getLang""?
Beans that might have unorthodox getter and setter methods, Dozer support user specified setter and getter methods.To make a bi-directional mapping in this case, look at the following example below.
The source field in element A specifies a custom setter method and getter method using attributes.
<field>
<a set-method="placeValue" get-method="buildValue">value</a>
<b>value</b>
</field>
What does the Dozer Bean Mapper do in this part? There is no other configuration, which describes, how to two collection should be mapped?
Understand the Custom set() and get() methods dozer documentation.
Lets take for example if we are mapping a String to an ArrayList by calling the addIntegerToList() method.
Note that this is defined as a one-way field type since we can not map an ArrayList to a String.
<!-- we can not map a ArrayList to a String,
hence the one-way mapping -->
<field type="one-way">
<a>integerStr</a>
<b set-method="addIntegerToList">integerList</b>
</field>
Does the Dozer Mapper map all fields automatically, which are founded by them if no configuration was set?
Yes, Dozer Mapper maps all the fields automatically from class-A to class-B iff both the field names are same.

Related

'this' is being passed a key for the map by Dozer mapper

I have an attribute which requires custom converter along with the parent context and the particular attribute in the mapper config has been configured like this. Let's call this as ChildMaper which converts from ChildSourceClass to ChildDestinationClass
<field custom-converter="MyCustomConvertorClass">
<a>this</a>
<b>targetField</b>
</field>
And the mapper has been invoked recursively by from another mapper associated with the a ParentClass.
The MyCustomConvertorClass could return null or a HashMap based on certain conditions.
When the conversion happens directly on the ChildSourceClass, the conversion happens as expected. But when the mapper is invoked on the ParentClass and when the convertor returns null, the mapper is setting a empty hashmap and adding key this to the attribute represented by targetField.
Any idea why this is happening?

Mapper for POJOs in Java

I am writing a mapper to map fields of 2 different POJOs in java. I have used Dozer mapper for simple mapping but in this case, I have a slightly complex strategy when it comes to set the value to the destination object. The getter is simple, but instead it setting it directly, I want to do some processing on the value.
My question is, can I use dozer mapper for my cause?
In case if its not possible, is it ok (from performance point of view) to use reflection to implement my own mapper (this is because I have defined the mapping in an xml file and dont want to hardcode it in the mapper class)?
<mapping>
<field>
<!-- source -->
<field-a name="cat">
<!-- destination -->
<field-b" name="dog">
</field>
</mapping>
Relevant to this xml, I want the following:
Source c;
Destination d;
d.setDog(someProcessing(c.getPsMessage()));
Note down the extra processing (someProcessing) after getting the value and before setting it to the destination object.
You can set your own getters-setters methods with Dozer. You can even map attributes with different type and name!:
Example:
<field>
<a>yourField</a>
<b set-method="nameOfYourFieldSetter(java.lang.String)">
nameOfYourFieldInOtherClass
</b>
</field>
Note down the extra processing (someProcessing) after getting the value and before setting it to the destination object.
You're overcomplicating things, use KISS principle:
Source c;
Destination d;
// String, Integer or what someProcessing returns!
String name = someProcessing(c.getPsMessage());
d.setDog(name);

SolrJ: Field mappings by mixin

Is it possible with SolrJ to define a bean and provide its field mapping by a mixin class when using QueryResponse#getBeans(Class)?
Example: Let E be an entity with a name attribute. The schema defines *_s_de and *_s_en dynamic fields.
Depending on a supplied language identifier I want to map the name_s_de or name_s_en field value to the name attribute of E without changing its implementation to have #Field("name_s_de") hardcoded.
Thanks!

Dozer Boolean mapping Eclipse error

I am using Dozer to map objects. But I am getting an wired error message
Property active for class com.edfx.adb.persist.entity.Customer cannot
be read from.
in the Dozer Mapping Editor of Eclipse, I have Dozer plugin installed in Eclipse. This is the mapping I have:
<mapping>
<class-a>com.edfx.adb.persist.entity.Customer</class-a>
<class-b>com.edfx.adb.web.dto.CustomerDTO</class-b>
<field>
<a get-method="isActive">active</a>
<b get-method="isActive">active</b>
<a-hint>java.lang.Boolean</a-hint>
<b-hint>java.lang.Boolean</b-hint>
</field>
</mapping>
Here active is the field with type boolean in Customer and CustomerDTO class.
I am unable to remove or hide the error message. Also don't know why it is showing. And for this error Eclipse showing error in whole project which is undesirable.
Any information would be very helpful to me.
I would try deleting hints. In this scenario I think you don´t need them.
But for best solution can you post your code for the entity and the DTO.

JAXB: unmarshalling xml with multiple names for the same element

I figure this will be easy for someone who really understands JAXB binding files...
Basic Question
How do you configure JAXB to unmarshal multiple elements into the same class?
Note: I want to avoid adding another dependency to my project (like MOXy). Ideally, this can be accomplished with annotations or a custom bindings file.
Background
I have an XML document that contains lots of variations of the same element--each with the exact same properties. Using my example below, all I care about is "Employees" but the XML specifies "directors, managers and staff." For our purposes, these are all subclasses of the same parent and we only need to work with the parent type (Employee) and our object model doesn't have or need instances of the subclasses.
I want JAXB to bind any instance of director, manager, or staff elements into an Employee object.
Example
input:
<organization>
<director>
<fname>Dan</fname>
<lname>Schman</lname>
</director>
<manager>
<fname>Joe</fname>
<lname>Schmo</lname>
</manager>
<staff>
<fname>Ron</fname>
<lname>Schwan</lname>
</staff>
<staff>
<fname>Jim</fname>
<lname>Schwim</lname>
</staff>
<staff>
<fname>Jon</fname>
<lname>Schwon</lname>
</staff>
</organization>
output:
After unmarshalling this example, I would end up with an Organization object with one property: List<Employees> employees where each employee only has a firstName and lastName.
(Note: each employee would be of type Employee NOT Director/Manager/Staff. Subclass information would be lost when unmarshalling. We also don't care about marshaling back out--we only need to create objects from XML)
Can this be done without extensions like MOXy? Can a custom bindings.xjb file save the day?
This corresponds to a choice structure. You could use an #XmlElements annotation for this use case:
#XmlElements({
#XmlElement(name="director", type=Employee.class),
#XmlElement(name="manager", type=Employee.class)
})
List<Employee> getEmployees() {
return employees;
}
If you are starting from an XML schema the following will help:
http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html

Categories