【发布时间】:2015-05-01 20:48:49
【问题描述】:
我正在以下模型之间进行映射:
@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;
【问题讨论】:
-
可以添加
AccountTypeDTO的代码吗? -
@Jens 请看编辑
-
您的问题是什么?对我来说,
AccountDTO中的accountType不是AccountType似乎很明显,因此出现了错误。 -
@RC。我的问题是如何有效地转换它们