将数据传输对象映射到域循环依赖关系



我正在尝试找到一种方法来创建这些映射器,而不会遇到循环依赖问题。我想用一个干净的解决方案来制作它,并且出于测试原因我不想使方法静态。有什么好主意吗?

@Entity
@Table(name = "Customers")
public class CustomerT {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "custmerId")
private Long customerId;
@Column(name = "Name", nullable = false)
private String name;
@Column(name = "Surname", nullable = false)
private String surname;
@Column(name = "Age")
private int age;
@OneToMany(mappedBy = "orders",  cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<OrderT> orderTSet;
public Long getCustomerId() {
return customerId;
}
@Entity
@Table(name = "Orders")
public class OrderT {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long orderId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(columnDefinition = "custmerId", referencedColumnName = "custmerId")
private CustomerT customerT;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "OrdersProducts",
joinColumns = @JoinColumn(name = "orderId"),
inverseJoinColumns = @JoinColumn(name = "productId"))
private List<ProductT> productTList;
public class CustomerMapper {
@Inject
OrderMapper orderMapper;
public CustomerD mapEntityToDomain(final CustomerT customerT) {
final CustomerD customerD = new CustomerD();
customerD.setCustomerId(customerT.getCustomerId());
customerD.setName(customerT.getName());
customerD.setAge(customerT.getAge());
customerD.setSurname(customerT.getSurname());
customerD.setOrderDSet(orderMapper.mapSetEntityToDomain(customerT.getOrderTSet()));
return customerD;
}
public class OrderMapper {
@Inject
ProductMapper productMapper;
@Inject
CustomerMapper customerMapper;
public OrderD mapEntityToDomain(final OrderT orderT) {
final OrderD orderD = new OrderD();
orderD.setId(orderT.getOrderId());
orderD.setCustomerD(customerMapper.mapEntityToDomain(orderT.getCustomerT()));
orderD.setProductDList(productMapper.mapListEntityToDomain(orderT.getProductTList()));
return orderD;
}

"org.jboss.weld.exceptions.DeploymentException: WELD-001443: 伪作用域 Bean 具有循环依赖关系。">

看看你的域对象,看起来你的 CustomerD 指向他/她的 OrdersD,OrdersD 指向 CustomerD。据我所知,CoustomerD -> OrderD 之间的强关系和从 OrderD 到 CustomerD 的引用看起来只是方便你的东西。

在这种情况下,OrderD 映射器不应自行创建新的 CustomerD。这样做将复制实体(每个订单将生成 CustomerD 的新实例(,这不是您想要的。

只需为 OrderMapper 创建一个只映射 Order 的新方法:

public class OrderMapper {
@Inject
ProductMapper productMapper;
@Inject
CustomerMapper customerMapper;
public OrderD mapEntityToDomainJustEntity(final OrderT orderT) {
final OrderD orderD = new OrderD();
orderD.setId(orderT.getOrderId());
return orderD;
}
public OrderD mapEntityToDomain(final OrderT orderT) {
final OrderD orderD = mapEntityToDomainJustEntity(orderT);         
orderD.setCustomerD(customerMapper.mapEntityToDomain(orderT.getCustomerT()));
return orderD;
}

并从您的客户映射器调用此新方法。然后将每个订单 D 的客户设置为"this">

public class CustomerMapper {
@Inject
OrderMapper orderMapper;
public CustomerD mapEntityToDomain(final CustomerT customerT) {
final CustomerD customerD = new CustomerD();
customerD.setCustomerId(customerT.getCustomerId());
customerD.setName(customerT.getName());
customerD.setAge(customerT.getAge());
customerD.setSurname(customerT.getSurname());
customerD.setOrderDSet(orderMapper.mapSetEntityToDomainJustEntity(customerT.getOrderTSet()));
// foreach OrderD -> orderD.setCustomerD(this);
return customerD;
}

最新更新