英文:
Mapping multiple fields to one in Mapstruct?
问题
我需要将一个开放银行类映射到我的核心类。我正在使用 `mapstruct`。我需要将多个枚举字段映射到我核心类中的一个字段。但我认为 mapstruct 不支持映射多个字段。是否有巧妙的方法或解决方案呢?
提前感谢!
以下是开放银行的枚举值:
```java
public enum Category {
TRANS_AND_SAVINGS_ACCOUNTS("TRANS_AND_SAVINGS_ACCOUNTS"),
REGULATED_TRUST_ACCOUNTS("REGULATED_TRUST_ACCOUNTS"),
CRED_AND_CHRG_CARDS("CRED_AND_CHRG_CARDS"),
PERS_LOANS("PERS_LOANS"),
MARGIN_LOANS("MARGIN_LOANS"),
PROPERTY_LOANS("PROPERTY_LOANS"),
TRADE_FINANCE("TRADE_FINANCE"),
OVERDRAFTS("OVERDRAFTS"),
BUSINESS_LOANS("BUSINESS_LOANS"),
RESIDENTIAL_MORTGAGES("RESIDENTIAL_MORTGAGES");
...
}
以下是我的核心类:
public enum Type{
CHARGES("Charges"),
CREDITS("Credits"),
ALL_LOANS("AllLoans"),
MORTGAGE("Mortgage"),
OTHER("Other"),
PREPAIDCARD("PrePaidCard"),
SAVINGS("Savings");
...
}
我有一个如下的 valueMapping
(是错误的):
public interface TypeMapper {
@ValueMappings( {
@ValueMapping(source = "CHARGES", target= "CRED_AND_CHRG_CARDS"),
@ValueMapping(source = "CREDITS", target= "CRED_AND_CHRG_CARDS"),
@ValueMapping(source = "ALL_LOANS", target = "PERS_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "MARGIN_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "BUSINESS_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "PROPERTY_LOANS"),
@ValueMapping(source = "MORTGAGE", target= "RESIDENTIAL_MORTGAGES"),
@ValueMapping(source = "OTHER", target= "OVERDRAFTS"),
@ValueMapping(source = "PREPAIDCARD", target = MappingConstants.NULL),
@ValueMapping(source = "SAVINGS", target = "TRANS_AND_SAVINGS_ACCOUNTS")
} )
...
}
它不接受将 PERS_LOANS("PERS_LOANS"), MARGIN_LOANS("MARGIN_LOANS"), PROPERTY_LOANS("PROPERTY_LOANS")
映射到 ALL_LOANS
。我想知道是否有一种方法可以将这三个值映射到一个字段中。也许可以在 @AfterMapping
中完成,有什么建议吗?我想将所有未映射的值映射到我的核心类中的 OTHER
。
<details>
<summary>英文:</summary>
I need to map from an open banking class to my core class. I am working with `mapstruct`. I need to map multiple enum fields to one on my core class. But i think mapstruct doesn't support mapping multiple fields. Any tricky way or a solution for that?
Thanks in advance!
Here is my enum values from open banking:
```java
public enum Category {
TRANS_AND_SAVINGS_ACCOUNTS("TRANS_AND_SAVINGS_ACCOUNTS"),
REGULATED_TRUST_ACCOUNTS("REGULATED_TRUST_ACCOUNTS"),
CRED_AND_CHRG_CARDS("CRED_AND_CHRG_CARDS"),
PERS_LOANS("PERS_LOANS"),
MARGIN_LOANS("MARGIN_LOANS"),
PROPERTY_LOANS("PROPERTY_LOANS"),
TRADE_FINANCE("TRADE_FINANCE"),
OVERDRAFTS("OVERDRAFTS"),
BUSINESS_LOANS("BUSINESS_LOANS"),
RESIDENTIAL_MORTGAGES("RESIDENTIAL_MORTGAGES");
...
}
And here is my core class:
public enum Type{
CHARGES( "Charges" ),
CREDITS( "Credits" ),
ALL_LOANS( "AllLoans" ),
MORTGAGE( "Mortgage" ),
OTHER( "Other" ),
PREPAIDCARD( "PrePaidCard" ),
SAVINGS( "Savings" );
...
}
And i have a valueMapping
as following(which is wrong):
public interface TypeMapper {
@ValueMappings( {
@ValueMapping(source = "CHARGES", target= "CRED_AND_CHRG_CARDS"),
@ValueMapping(source = "CREDITS", target= "CRED_AND_CHRG_CARDS"),
@ValueMapping(source = "ALL_LOANS", target = "PERS_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "MARGIN_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "BUSINESS_LOANS"),
@ValueMapping(source = "ALL_LOANS", target = "PROPERTY_LOANS"),
@ValueMapping(source = "MORTGAGE", target= "RESIDENTIAL_MORTGAGES"),
@ValueMapping(source = "OTHER", target= "OVERDRAFTS"),
@ValueMapping(source = "PREPAIDCARD", target = MappingConstants.NULL),
@ValueMapping(source = "SAVINGS", target = "TRANS_AND_SAVINGS_ACCOUNTS")
} )
...
}
It does not accept to map PERS_LOANS("PERS_LOANS"), MARGIN_LOANS("MARGIN_LOANS"), PROPERTY_LOANS("PROPERTY_LOANS")
to ALL_LOANS
. I wonder if there's a way to map all 3 to one field. Maybe can it be covered in @AfterMapping
, any suggestions? And i want to map all the unmapped values to OTHER
in my core class.
答案1
得分: 0
你必须自己实现它,因为 MapStruct 不支持该功能。尝试使用 @qualifiedByName
注解与您自己的方法
例如:
public interface TypeMapper{
@Mapping(target= "CRED_AND_CHRG_CARDS", qualifiedByName = "creditAndChrgCardsMapping")
public Type CategoryToType(Category category);
default String qualifiedByName (String charges, String credits){
// 实现您的业务逻辑,例如
return charges + credits;
}
}
英文:
You have to implement it by yourself, because MapStruct doesn't support that. Try to use @qualifiedByName
annotation with your own method
For example:
public interface TypeMapper{
@Mapping(target= "CRED_AND_CHRG_CARDS", qualifiedByName = "creditAndChrgCardsMapping")
public Type CategoryToType(Category category);
default String qualifiedByName (String charges, String credits){
//Implement your business logic for example
return charges + credits;
}
}
答案2
得分: 0
在您的问题中,您在说您无法将 PERS_LOANS
、MARGIN_LOANS
等映射到 ALL_LOANS
。然而,您的值映射是颠倒的。您正在将 ALL_LOANS
映射到 PERS_LOANS
。
不可能将一个枚举(源)值映射到多个枚举(目标)值。然而,可以将多个枚举(源)值映射到同一个枚举(目标)值。
英文:
In your question you are saying that you can't map PERS_LOANS
, MARGIN_LOANS
, etc. to ALL_LOANS
. However, your value mappings are reversed. You are mapping ALL_LOANS
to PERS_LOANS
.
It is not possible to map one enum (source) value to multiple enum (target) values. However, it is possible to map multiple enum (source) values to the same enum (target) value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论