在模型和 DTO 之间映射对象的问题

Problems in mapping objects between the model and DTO

我在以下模型之间映射:

@Entity
@Table(name="account_type")
@NamedQuery(name="AccountType.findAll", query="SELECT a FROM AccountType a")
public class AccountType implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="account_type_id")
    private Integer accountTypeId;

    @Column(name="account_type_code")
    private String accountTypeCode;

    @OneToMany(mappedBy="accountType")
    private Set<Account> accounts;

其中有一组Account:

@Entity
@NamedQuery(name="Account.findAll", query="SELECT a FROM Account a")
public class Account implements Serializable {
    private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name="account_id")
private Integer accountId;

@Column(name="account_number")
private String accountNumber;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="account_type_id_fk")
private AccountType accountType;

和他们的 DTO.

我在映射 Account:

等复杂类型时遇到问题
public static Account getAccount(AccountDTO dto) {
        Account model = new Account();
        
        model.setAccountId(dto.getAccountId());
        model.setAccountNumber(dto.getAccountNumber());
        
        model.setAccountType(dto.getAccountType());
        // Error: can't convert from AccountypeDTO to AccountType

        return model;
    }

它给出了无法从 AccountypeDTO 转换为 AccountType

的错误

所以我做了以下事情:

model.setAccountType(getAccountType(dto.getAccountType()));

其中getAccountType方法是:

public static AccountType getAccountType(AccountTypeDTO dto) {
        
        AccountType model = new AccountType();

        model.setAccountTypeId(dto.getAccountTypeId());
        model.setAccountTypeCode(dto.getAccountTypeCode());

        model.setAccounts(dto.getAccounts());
        // Now here again a similar error
    }

我觉得很深recursive?如何解决?

我的问题是如何有效地转换它们。

Annex


acountTypeDTO的代码:

@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class AccountTypeDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    @NotNull
    @NotEmpty
    private Integer accountTypeId;

    @NotNull
    @NotEmpty
    private String accountTypeCode;

    private Set<AccountDTO> accounts;

AccountDTO的代码:

@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class AccountDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    @NotNull
    @NotEmpty
    private Integer accountId;

    @NotNull
    @NotEmpty
    private String accountNumber;

    private AccountTypeDTO accountType;

我想到了两种替代方法,但它们需要进行一些更改。我会先说我还没有遇到转换为 DTO(即使我正在进行深度递归转换)是瓶颈的情况。即使您的性能要求或规模如此之大以至于它确实成为瓶颈,那么我个人建议在我开始担心性能到那个细节级别之前将工作分配给多个服务器。此外,它可能 看起来 效率低下但性能很少直观,您是否确认此转换是瓶颈?

第一种选择是不使用单独的 classes 作为 DTO。有些方法使用相同的 class 作为 DTO 和底层实体,有些方法使用 DTO 作为父 class 和实体 class 作为子 class。这将使您不必进行任何类型的 DTO<-> 实体转换。有缺点,因为这几乎总是将两个职责组合成一个 class,它会使您的代码更复杂且可读性更差。

第二种选择不是 return 帐户本身,而是将它们转换为 ID。在这种方法中,您的 AccountTypeDTO 将具有 Set<Integer> accountIds 而不是 Set<AccountDTO> accounts。但是,这仅适用于您的客户并不总是需要对每个帐户进行操作的情况。