Orika - 不生成字段映射;只有 byDefault 有效

Orika - Does not generate field-mapping; only byDefault works

我刚刚开始使用 Orika,我想将数据从我的应用程序 ('PaymentPlan') 中使用的业务实体映射到我用于数据传输的 POJO ('ShopifyRecurringApplicationCharge' ).

一切似乎都工作正常(甚至是 String<->Date 的自定义转换器),除了字段..

从 'ShopifyRecurr..' 转换为 'PaymentPlan' 时,我希望字符串得到解析并转换为 'int',但字段值保持为“0”。相反,'ShopifyRecurr...ChargeInner.ID' 只是保持为空。

编辑: 我查看了 Orika 生成的映射器,发现属性 shopifyID/id 没有代码,尽管我已经在Orika-映射。我还在下面附上了生成的源代码。

我是不是犯了什么愚蠢的错误?

付款计划

package de.dpt.persistence;

import java.io.Serializable;
import javax.persistence.*;
@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name="PaymentPlan")
public class PaymentPlan implements Serializable {
public PaymentPlan() {
}


@Column(name="ID", nullable=false, length=11)   
@Id 
@GeneratedValue(generator="DE_DPT_PERSISTENCE_PAYMENTPLAN_ID_GENERATOR")    
@org.hibernate.annotations.GenericGenerator(name="DE_DPT_PERSISTENCE_PAYMENTPLAN_ID_GENERATOR", strategy="native")  
private int ID;
@Column(name="`Plan`", nullable=false, length=11)   
private int plan;

@Column(name="Activated_on", nullable=true, length=255) 
@Temporal(TemporalType.DATE)    
private java.util.Date activated_on;

@Column(name="Billing_on", nullable=true)   
@Temporal(TemporalType.DATE)    
private java.util.Date billing_on;

@Column(name="Cancelled_on", nullable=true) 
@Temporal(TemporalType.DATE)    
private java.util.Date cancelled_on;

@Column(name="Confirmation_url", nullable=true, length=255) 
private String confirmation_url;

@Column(name="Created_at", nullable=true)   
@Temporal(TemporalType.DATE)    
private java.util.Date created_at;

@Column(name="ShopifyID", nullable=false, length=11)    
private int shopifyID;

@Column(name="Name", nullable=true, length=255) 
private String name;

@Column(name="Price", nullable=false, length=255)   
private String price;

@Column(name="Return_url", nullable=true, length=255)   
private String return_url;

@Column(name="Status", nullable=true, length=255)   
private String status;

@Column(name="Terms", nullable=true, length=255)    
private String terms;

@Column(name="Test", nullable=false, length=1)  
private boolean test;

@Column(name="Trial_days", nullable=false, length=11)   
private int trial_days;

@Column(name="Trial_ends_on", nullable=true)    
@Temporal(TemporalType.DATE)    
private java.util.Date trial_ends_on;

@Column(name="Updated_at", nullable=true)   
@Temporal(TemporalType.DATE)    
private java.util.Date updated_at;

private void setID(int value) {
    this.ID = value;
}

public int getID() {
    return ID;
}

public int getORMID() {
    return getID();
}

/**
 * ET_PaymentPlan
 */
public void setPlan(int value) {
    this.plan = value;
}

/**
 * ET_PaymentPlan
 */
public int getPlan() {
    return plan;
}

public void setActivated_on(java.util.Date value) {
    this.activated_on = value;
}

public java.util.Date getActivated_on() {
    return activated_on;
}

public void setBilling_on(java.util.Date value) {
    this.billing_on = value;
}

public java.util.Date getBilling_on() {
    return billing_on;
}

public void setCancelled_on(java.util.Date value) {
    this.cancelled_on = value;
}

public java.util.Date getCancelled_on() {
    return cancelled_on;
}

public void setConfirmation_url(String value) {
    this.confirmation_url = value;
}

public String getConfirmation_url() {
    return confirmation_url;
}

public void setCreated_at(java.util.Date value) {
    this.created_at = value;
}

public java.util.Date getCreated_at() {
    return created_at;
}

public void setShopifyID(int value) {
    this.shopifyID = value;
}

public int getShopifyID() {
    return shopifyID;
}

public void setName(String value) {
    this.name = value;
}

public String getName() {
    return name;
}

public void setPrice(String value) {
    this.price = value;
}

public String getPrice() {
    return price;
}

public void setReturn_url(String value) {
    this.return_url = value;
}

public String getReturn_url() {
    return return_url;
}

public void setStatus(String value) {
    this.status = value;
}

public String getStatus() {
    return status;
}

public void setTerms(String value) {
    this.terms = value;
}

public String getTerms() {
    return terms;
}

public void setTest(boolean value) {
    this.test = value;
}

public boolean getTest() {
    return test;
}

public void setTrial_days(int value) {
    this.trial_days = value;
}

public int getTrial_days() {
    return trial_days;
}

public void setTrial_ends_on(java.util.Date value) {
    this.trial_ends_on = value;
}

public java.util.Date getTrial_ends_on() {
    return trial_ends_on;
}

public void setUpdated_at(java.util.Date value) {
    this.updated_at = value;
}

public java.util.Date getUpdated_at() {
    return updated_at;
}

public String toString() {
    return String.valueOf(getID());
}
}

ShopifyRecurringApplicationCharge

package de.shopify.api.model;

import java.util.List;

public class ShopifyRecurringApplicationCharge {

public ShopifyRecurringApplicationChargeInner
recurring_application_charge;
public List<ShopifyRecurringApplicationChargeInner>
recurring_application_charges;

public static class ShopifyRecurringApplicationChargeInner {

    public ShopifyRecurringApplicationChargeInner()  {}

    public String activated_on;
    public String billing_on;
    public String cancelled_on;
    public String capped_amount;
    public String confirmation_url;
    public String created_at;
    public String id;
    public String name;
    public String price;
    public String return_url;
    public String status;
    public String terms;
    public String test;
    public String trial_days;
    public String trial_ends_on;
    public String updated_at;

}
}

映射

public enum PaymentPlanMapper {
INSTANCE;

private PaymentPlanMapper()  {
    ShopifyBaseMapper.factory.classMap(PaymentPlan.class, ShopifyRecurringApplicationChargeInner.class)
    .fieldBToA("id","shopifyID")
    .exclude("ID")
    .exclude("plan")
    .byDefault()
    .register();
}

} 

使用填充的 ShopifyRecurringApplicationCharge(Inner),我使用以下映射调用:

PaymentPlan newPaymentPlan = mapperFacadeOrika.map(postResponse.recurring_application_charge, PaymentPlan.class);

编辑:生成的映射器

package ma.glasnost.orika.generated;

public class Orika_ShopifyRecurringApplicationChargeInner_PaymentPlan_Mapper4020303591863[=14=]
        extends ma.glasnost.orika.impl.GeneratedMapperBase {

    public void mapAtoB(java.lang.Object a, java.lang.Object b,
            ma.glasnost.orika.MappingContext mappingContext) {

        super.mapAtoB(a, b, mappingContext);

        de.dpt.persistence.PaymentPlan source = ((de.dpt.persistence.PaymentPlan) a);
        de.shopify.api.model.ShopifyRecurringApplicationCharge.ShopifyRecurringApplicationChargeInner destination = ((de.shopify.api.model.ShopifyRecurringApplicationCharge.ShopifyRecurringApplicationChargeInner) b);

        if ((!(((java.util.Date) source.getActivated_on()) == null))) {
            destination.activated_on = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getActivated_on()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.activated_on = null;
        }
        if ((!(((java.util.Date) source.getBilling_on()) == null))) {
            destination.billing_on = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getBilling_on()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.billing_on = null;
        }
        if ((!(((java.util.Date) source.getCancelled_on()) == null))) {
            destination.cancelled_on = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getCancelled_on()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.cancelled_on = null;
        }
        if ((!(((java.lang.String) source.getConfirmation_url()) == null))) {
            destination.confirmation_url = ""
                    + ((java.lang.String) source.getConfirmation_url());
        } else {
            destination.confirmation_url = null;
        }
        if ((!(((java.util.Date) source.getCreated_at()) == null))) {
            destination.created_at = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getCreated_at()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.created_at = null;
        }
        if ((!(((java.lang.String) source.getName()) == null))) {
            destination.name = "" + ((java.lang.String) source.getName());
        } else {
            destination.name = null;
        }
        if ((!(((java.lang.String) source.getPrice()) == null))) {
            destination.price = "" + ((java.lang.String) source.getPrice());
        } else {
            destination.price = null;
        }
        if ((!(((java.lang.String) source.getReturn_url()) == null))) {
            destination.return_url = ""
                    + ((java.lang.String) source.getReturn_url());
        } else {
            destination.return_url = null;
        }
        if ((!(((java.lang.String) source.getStatus()) == null))) {
            destination.status = "" + ((java.lang.String) source.getStatus());
        } else {
            destination.status = null;
        }
        if ((!(((java.lang.String) source.getTerms()) == null))) {
            destination.terms = "" + ((java.lang.String) source.getTerms());
        } else {
            destination.terms = null;
        }
        destination.test = ""
                + ((ma.glasnost.orika.Converter) usedConverters[0])
                        .convert(
                                java.lang.Boolean.valueOf(((boolean) source
                                        .getTest())),
                                ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        destination.trial_days = ""
                + ((ma.glasnost.orika.Converter) usedConverters[0]).convert(
                        java.lang.Integer
                                .valueOf(((int) source.getTrial_days())),
                        ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        if ((!(((java.util.Date) source.getTrial_ends_on()) == null))) {
            destination.trial_ends_on = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getTrial_ends_on()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.trial_ends_on = null;
        }
        if ((!(((java.util.Date) source.getUpdated_at()) == null))) {
            destination.updated_at = ""
                    + ((ma.glasnost.orika.Converter) usedConverters[0])
                            .convert(
                                    ((java.util.Date) source.getUpdated_at()),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]));
        } else {
            destination.updated_at = null;
        }
        if (customMapper != null) {
            customMapper.mapAtoB(source, destination, mappingContext);
        }
    }

    public void mapBtoA(java.lang.Object a, java.lang.Object b,
            ma.glasnost.orika.MappingContext mappingContext) {

        super.mapBtoA(a, b, mappingContext);

        de.shopify.api.model.ShopifyRecurringApplicationCharge.ShopifyRecurringApplicationChargeInner source = ((de.shopify.api.model.ShopifyRecurringApplicationCharge.ShopifyRecurringApplicationChargeInner) a);
        de.dpt.persistence.PaymentPlan destination = ((de.dpt.persistence.PaymentPlan) b);

        if ((!(((java.lang.String) source.activated_on) == null))) {
            destination
                    .setActivated_on(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.activated_on),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setActivated_on(null);
        }
        if ((!(((java.lang.String) source.billing_on) == null))) {
            destination
                    .setBilling_on(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.billing_on),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setBilling_on(null);
        }
        if ((!(((java.lang.String) source.cancelled_on) == null))) {
            destination
                    .setCancelled_on(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.cancelled_on),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setCancelled_on(null);
        }
        if ((!(((java.lang.String) source.confirmation_url) == null))) {
            destination.setConfirmation_url(""
                    + ((java.lang.String) source.confirmation_url));
        } else {
            destination.setConfirmation_url(null);
        }
        if ((!(((java.lang.String) source.created_at) == null))) {
            destination
                    .setCreated_at(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.created_at),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setCreated_at(null);
        }
        if ((!(((java.lang.String) source.name) == null))) {
            destination.setName("" + ((java.lang.String) source.name));
        } else {
            destination.setName(null);
        }
        if ((!(((java.lang.String) source.price) == null))) {
            destination.setPrice("" + ((java.lang.String) source.price));
        } else {
            destination.setPrice(null);
        }
        if ((!(((java.lang.String) source.return_url) == null))) {
            destination.setReturn_url(""
                    + ((java.lang.String) source.return_url));
        } else {
            destination.setReturn_url(null);
        }
        if ((!(((java.lang.String) source.status) == null))) {
            destination.setStatus("" + ((java.lang.String) source.status));
        } else {
            destination.setStatus(null);
        }
        if ((!(((java.lang.String) source.terms) == null))) {
            destination.setTerms("" + ((java.lang.String) source.terms));
        } else {
            destination.setTerms(null);
        }
        if ((!(((java.lang.String) source.test) == null))) {
            destination
                    .setTest(java.lang.Boolean
                            .valueOf(
                                    ""
                                            + ((ma.glasnost.orika.Converter) usedConverters[2])
                                                    .convert(
                                                            ((java.lang.String) source.test),
                                                            ((ma.glasnost.orika.metadata.Type) usedTypes[2])))
                            .booleanValue());
        }
        if ((!(((java.lang.String) source.trial_days) == null))) {
            destination
                    .setTrial_days(java.lang.Integer
                            .valueOf(
                                    ""
                                            + ((ma.glasnost.orika.Converter) usedConverters[2])
                                                    .convert(
                                                            ((java.lang.String) source.trial_days),
                                                            ((ma.glasnost.orika.metadata.Type) usedTypes[3])))
                            .intValue());
        }
        if ((!(((java.lang.String) source.trial_ends_on) == null))) {
            destination
                    .setTrial_ends_on(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.trial_ends_on),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setTrial_ends_on(null);
        }
        if ((!(((java.lang.String) source.updated_at) == null))) {
            destination
                    .setUpdated_at(((java.util.Date) ((ma.glasnost.orika.Converter) usedConverters[1])
                            .convert(
                                    ((java.lang.String) source.updated_at),
                                    ((ma.glasnost.orika.metadata.Type) usedTypes[1]))));
        } else {
            destination.setUpdated_at(null);
        }
        if (customMapper != null) {
            customMapper.mapBtoA(source, destination, mappingContext);
        }
    }

}

我终于意识到问题不在Orika,而是我的PaymentPlanMapper实现和enum-singleton设计模式不合适。

结果是

  • PaymentPlannerMapper 从未初始化
  • 我的自定义映射从未在 MapperFactory 注册
  • 当 facade.map(..) 被调用
  • 时,Okira 自动为 PaymentPlan/ShopifyRecurring... 动态生成了一个映射器

Orika 的自动生成让我很难发现我自己的错误,因为 orika 所做的大部分事情看起来都不错。

我只是在调试自动生成的映射器 ('Orika_ShopifyRecurringApplicationCharge_PaymentPlan_Mapper7011935565727[=41=]') 并发现属性 'fromAutoMapping' 为 TRUE 时才意识到哪里出了问题。因此,对于类似的问题,查看生成的映射器源是有意义的。

调试生成的映射器

为了启用生成的映射器的源代码和调试,我使用了 Orika Documentation 中的 'Enable step-debugging' 和 'Generate Source and/or Class Files.' 部分。

文档基本上转换为在应用程序启动时调用以下代码:

System.setProperty(OrikaSystemProperties.COMPILER_STRATEGY,EclipseJdtCompilerStrategy.class.getName());
System.setProperty(OrikaSystemProperties.WRITE_SOURCE_FILES,"true");
System.setProperty(OrikaSystemProperties.WRITE_CLASS_FILES,"true");

正确的映射器

我现在已经用 ..

替换了我的映射器
@Component
public class PaymentPlanMapper {

    private PaymentPlanMapper()  {
        ShopifyBaseMapper.factory.classMap(PaymentPlan.class, ShopifyRecurringApplicationCharge.class)
        .field("shopifyID","id")
        .byDefault()
        .register();
    }

} 

.. 使用 Spring 而不是 Enum-Singleton 模式。