gpt4 book ai didi

java - Orika - 不生成字段映射;仅按默认工作

转载 作者:行者123 更新时间:2023-11-30 02:41:32 26 4
gpt4 key购买 nike

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

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

  • PaymentPlan.shopifyID(整数)
  • ShopifyRecurringApplicationChargeInner.id(字符串)

从“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$0
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 注册
  • Okira 已在调用facade.map(..) 时动态自动生成 PaymentPlan/ShopifyRecurring... 的映射器

Orika 的自动生成使我自己的错误很难被发现,因为 Orika 所做的大多数事情看起来都不错。

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

调试生成的映射器

为了启用生成的映射器的源和调试,我使用了“启用单步调试”和“生成源和/或类文件”部分。在 Orika Documentation .

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

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 模式。

关于java - Orika - 不生成字段映射;仅按默认工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com