当我尝试使用 MapStruct 将 List<Entity> 转换为 Dto 时出现错误
I'm getting error when i tried List<Entity> to Dto with MapStruct
public class Account {
private String accountId
private List<Transaction> transactions = new ArrayList<>();
public abstract class Transaction {
private double amount;
public class AccountEntity extends BaseEntity{
@OneToMany
private List<TransactionEntity> transactions;
public class TransactionEntity extends BaseEntity{
@Id
private String transactionId;
private double amount;
private String type;
@ManyToOne
@JoinColumn(name = "accountId", referencedColumnName = "accountNumber")
private AccountEntity account;
@Mapper(componentModel = "spring")
public interface AccountMapper {
Account AccountEntityToAccount(AccountEntity accountEntity);
AccountEntity AccountToAccountEntity(Account account);
我的 AccountEntityToAccount 方法出现错误
return 类型的 Transaction 是一个抽象 class 或接口。提供非抽象/非接口结果类型或工厂方法。
帐户 AccountEntityToAccount(AccountEntity accountEntity);
我该如何解决这个问题。我的交易 class 应该是抽象的,我无法更改抽象关键字
问题是
private List<Transaction> transactions = new ArrayList<>();
如果您有另一个 class 扩展 Transaction 然后将其用于列表中的通用。否则 Mapstuct now which class 应该如何为那个 List
创建
添加一个方法以从提供的 TransactionEntity 创建正确的交易。
阅读代码您可能希望有如下内容:
Transaction fromTransactionEntity(TransactionEntity entity){
if (entity == null){
return null;
}
if ( entity.getType().equals("something") ) {
return toSomethingTransaction(entity);
}
if ( entity.getType().equals("other") ) {
return toOtherTransaction(entity);
}
throw new IllegalStateException("unsupported transaction found, cannot instantiate object.");
}
SomethingTransaction toSomethingTransaction(TransactionEntity entity);
OtherTransaction toOtherTransaction(TransactionEntity entity);
在另一个方向上,您可能想做同样的事情。 (当 1.5.0
发布时,您可以使用 @SubclassMapping
而不是手动编写 instanceof。目前处于 1.5.0.RC1
阶段。)
TransactionEntity toTransactionEntity(Transaction transaction){
if (transaction == null){
return null;
}
if (transaction instanceof SomethingTransaction){
return fromSomethingTransaction(transaction);
}
if (transaction instanceof OtherTransaction){
return toOtherTransaction(entity);
}
throw new IllegalStateException("unsupported transaction found, cannot store it.");
}
@Mapping( target = "type", constant = "something" )
TransactionEntity fromSomethingTransaction(SomethingTransaction transaction);
@Mapping( target = "type", constant = "other" )
TransactionEntity fromOtherTransaction(OtherTransaction transaction);
public class Account {
private String accountId
private List<Transaction> transactions = new ArrayList<>();
public abstract class Transaction {
private double amount;
public class AccountEntity extends BaseEntity{
@OneToMany
private List<TransactionEntity> transactions;
public class TransactionEntity extends BaseEntity{
@Id
private String transactionId;
private double amount;
private String type;
@ManyToOne
@JoinColumn(name = "accountId", referencedColumnName = "accountNumber")
private AccountEntity account;
@Mapper(componentModel = "spring")
public interface AccountMapper {
Account AccountEntityToAccount(AccountEntity accountEntity);
AccountEntity AccountToAccountEntity(Account account);
我的 AccountEntityToAccount 方法出现错误 return 类型的 Transaction 是一个抽象 class 或接口。提供非抽象/非接口结果类型或工厂方法。 帐户 AccountEntityToAccount(AccountEntity accountEntity);
我该如何解决这个问题。我的交易 class 应该是抽象的,我无法更改抽象关键字
问题是
private List<Transaction> transactions = new ArrayList<>();
如果您有另一个 class 扩展 Transaction 然后将其用于列表中的通用。否则 Mapstuct now which class 应该如何为那个 List
创建添加一个方法以从提供的 TransactionEntity 创建正确的交易。
阅读代码您可能希望有如下内容:
Transaction fromTransactionEntity(TransactionEntity entity){
if (entity == null){
return null;
}
if ( entity.getType().equals("something") ) {
return toSomethingTransaction(entity);
}
if ( entity.getType().equals("other") ) {
return toOtherTransaction(entity);
}
throw new IllegalStateException("unsupported transaction found, cannot instantiate object.");
}
SomethingTransaction toSomethingTransaction(TransactionEntity entity);
OtherTransaction toOtherTransaction(TransactionEntity entity);
在另一个方向上,您可能想做同样的事情。 (当 1.5.0
发布时,您可以使用 @SubclassMapping
而不是手动编写 instanceof。目前处于 1.5.0.RC1
阶段。)
TransactionEntity toTransactionEntity(Transaction transaction){
if (transaction == null){
return null;
}
if (transaction instanceof SomethingTransaction){
return fromSomethingTransaction(transaction);
}
if (transaction instanceof OtherTransaction){
return toOtherTransaction(entity);
}
throw new IllegalStateException("unsupported transaction found, cannot store it.");
}
@Mapping( target = "type", constant = "something" )
TransactionEntity fromSomethingTransaction(SomethingTransaction transaction);
@Mapping( target = "type", constant = "other" )
TransactionEntity fromOtherTransaction(OtherTransaction transaction);