简体   繁体   English

推土机深度映射字段为String

[英]Dozer deep mapping field as String

I have an object that is entirely too complicated. 我的对象太复杂了。

public class OrderItem {
    private Survey survey;
    ...
}

public class Survey {
    private QuestionAnswerGroup questionAnswerGroup;
    ...
}

public class QuestionAnswerGroup {
    private Map<String, List<QuestionAnswerSet>> questionAnswerSets;
    ...
}

My goal is to flatten the questionAnswerSets map so that all of the lists in the values are consolidated into one as 我的目标是展平questionAnswerSets映射,以便将值中的所有列表合并为一个

public class OrderItemDTO {
    private List<OrderItemQuestionAnswer> survey;
    ...
}

Rather than try to figure out how to join all those lists into one in Dozer, which I'm not sure it can even do, I wrote a custom setter. 我写了一个自定义的二传手,而不是试图弄清楚如何在Dozer中将所有这些列表合并为一个(我不确定它是否能做到)。

<mapping wildcard="false">
    <class-a>OrderItem</class-a>
    <class-b>OrderItemDTO</class-b>
    <field>
        <a get-method="getSurvey" is-accessible="false">survey</a>
        <b set-method="makeFlatSurvey" get-method="makeDomainSurvey" is-accessible="false">survey</b>
    </field>
    ...
</mapping>

Which I would assume would mean that Dozer would simply read the Survey off of the first object and plop it into makeFlatSurvey but instead it is reading off the questionAnswerGroup field on the Survey object and then going through each item in the questionAnswerSets and replacing them with a String version of themselves. 我认为这意味着Dozer会简单地从第一个对象中读取Survey并将其放入makeFlatSurvey中,而是读取了Survey对象上的questionAnswerGroup字段,然后遍历questionAnswerSets中的每一项并将其替换为自己的字符串版本。 A breakpoint in the toString method on QuestionAnswerSet shows that Dozer is calling that as part of its process. QuestionAnswerSet上toString方法中的断点表明Dozer在其过程中正在调用该断点。

I'm stumped here on a way to force Dozer to just accept the Survey object without changing it. 我在这里陷入困境,迫使Dozer只接受Survey对象而不更改它。

The only way I've found to do this is to set copy-by-reference to true. 我发现这样做的唯一方法是copy-by-reference设置为true。

You haven't posted OrderItemQuestionAnswer class, but I'm assuming it's as follows: 您尚未发布OrderItemQuestionAnswer类,但是我假设它如下:

public class OrderItemQuestionAnswer {
    private String question;
    private String answer;  
    // Getters/setters and toString omitted
}

I'm also assuming OrderItemDTO.makeFlatSurvey looks something like this: 我还假设OrderItemDTO.makeFlatSurvey看起来像这样:

public void makeFlatSurvey(Survey survey)
{
    for (List<QuestionAnswerSet> questionAnswerSets : survey.getQuestionAnswerGroup().getQuestionAnswerSets().values())
    {
        for (QuestionAnswerSet questionAnswerSet : questionAnswerSets)
        {
            OrderItemQuestionAnswer orderItemQuestionAnswer = new OrderItemQuestionAnswer();
            orderItemQuestionAnswer.setQuestion(questionAnswerSet.getQuestion());
            orderItemQuestionAnswer.setAnswer(questionAnswerSet.getAnswer());
            this.survey.add(orderItemQuestionAnswer);
        }
    }
}

Modifying the field element in the mapping and adding copy-by-reference="true" will prevent the ClassCastException (I have no idea why): 在映射中修改field元素并添加copy-by-reference="true"将防止ClassCastException(我不知道为什么):

<mapping wildcard="false">
    <class-a>OrderItem</class-a>
    <class-b>OrderItemDTO</class-b>
    <field copy-by-reference="true">
        <a get-method="getSurvey" is-accessible="false">survey</a>
        <b set-method="makeFlatSurvey" get-method="makeDomainSurvey" is-accessible="false">survey</b>
    </field>
    ...
</mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM