当前位置: 首页 > 知识库问答 >
问题:

当我用MapStruct将List尝试到Dto时,出现了错误

燕宜修
2023-03-14
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);

My AccountEntityToAccount方法出现错误,返回类型Transaction是抽象类或接口。提供非抽象/非接口结果类型或工厂方法。AccountEntityToAccount(AccountEntity AccountEntity);

我怎样才能解决这个问题。我的事务类应该是抽象的,我无法更改抽象关键字

共有2个答案

佟涵畅
2023-03-14

问题是

private List<Transaction> transactions = new ArrayList<>();

如果您有另一个扩展Transaction的类,则将其用于列表中的泛型。否则,Mapstuct现在应该如何为该列表创建哪个类

慕宏博
2023-03-14

添加一个方法,从提供的事务实体中创建正确的事务。

阅读代码时,您可能希望拥有如下内容:

java prettyprint-override">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,而不是手动编写当前处于 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);
 类似资料:
  • 当我尝试从本机切换到Webview时,错误出现在代码集contextNames=driver。getContextHandles();for(String contextName:contextNames){ System.out.println(contextName);//打印出一些像原生应用程序、WEBVIEW_com.example.android等}String setContext=c

  • 基本上我是按照这个指示做的:http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/ 但结果是: 尚未找到任何服务器 请求方法:获取请求URL:http://52.25.226.143/admin/ Django版本:1.5.11异常类型:ServerSelectionTimeoutError异常

  • 我使用的是linux服务器,我已经安装了xamp服务器,当我尝试运行时/opt/lampp/lampp状态我发现正在启动Linux 7.2的XAMPP。3-0... XAMPP:正在启动Apache。。。失败XAMPP:另一个web服务器已在运行。XAMPP:正在启动MySQL。。。好啊XAMPP:正在启动ProFTPD。。。已经在运行了。在cli上 之后,我使用。/opt/lampp/lampp

  • 我有这个问题。我正在尝试连接到数据库并推送文本用户。当我打开文件时,我只会发现很多错误。您可以进一步查看错误和我的代码。仅供参考:我的数据库在MySQLi中 警告:mysqli::\uuuu construct():php\u network\u getaddresses:getaddrinfo失败:第3行的/www/webvol9/rj/fxgnq6r66hz6x2j/my domain/pub

  • 对于这个错误我该怎么做呢? 错误:任务“:app:DexDebug”执行失败。 当我尝试运行我的应用程序时,这个问题就会出现。

  • 我试图使用MapStruct创建一个“PersondTo”类到“PersonEntity”的映射器 无法将属性“java.util.List Cars”映射到“com.example.car.CarEntity Cars”。考虑声明/实现一个映射方法:“com.example.car.carEntity map(java.util.List value)”。 如果我从Person类中删除“加法器”