我是MapStruct的新手,無法在Google上找到我的問題的答案。 我有一個購物車已獲得樣品(其他屬性),每個樣品需要一個參考返回到我的ShoppingCart。是否可以使用MapStruct進行這種映射? 如果沒有MapStruct,我只需將對ShoppingCart的引用傳遞給Samples。這是手寫的:MapStruct反向鏈接映射可能嗎?
protected ShoppingCart map(Cart cart, DataShareOption dataShareOption) {
//(other stuff)
for (CartSample cartSample : cart.getCartSamples()) {
ShoppingCartSample sample = mapCartSample(cartSample, shoppingCart,
dataShareOption);
shoppingCart.getSamples().add(sample);
}
}
protected ShoppingCartSample mapCartSample(CartSample cartSample,
ShoppingCart shoppingCart, DataShareOption dataShareOption) {
ShoppingCartSample sample = new ShoppingCartSample();
sample.setShoppingCart(shoppingCart);
//(other stuff)
return sample;
}
// the classes declarations:
// business class
public class ShoppingCart extends ShoppingCartHeader
{
private List<ShoppingCartSample> samples = new ArrayList<ShoppingCartSample>();
//rest of the class
// data base class:
@Entity
@Table(name = "cart")
public class Cart extends BaseEntity
{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "cart")
private Set<CartSample> cartSamples = new HashSet<CartSample>();
// more stuff here
// business class:
public class ShoppingCartSample
{
private ShoppingCart shoppingCart;
// rest of the class
// data base class:
@Entity
@Table(name = "cart_sample")
public class CartSample
{
@ManyToOne()
@JoinColumn(name = "cart_id")
private Cart cart;
// more stuff here
是由您或MapStruct創建問題的方法? – Filip
'ShoppingCart'包含'ShoppingCartSample'(s)嗎?你有沒有想要映射的週期?你能否展示你的模型的簡化代碼?我需要這個信息,所以我可以最正確地回答這個問題,我不知道我現在是否正確理解它。 – Filip
是的,ShoppingCart包含ShoppingCarSamples。 ShoppingCartSample包含ShoppingCart。我想這可以稱爲一個循環。 –