我开始向您展示我的场景。
这是我的父对象:
@Entity
@Table(name="cart")
public class Cart implements Serializable{
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CartItem> cartItems;
...
}
这是我的孩子对象:
@Entity
@Table(name="cart_item")
public class CartItem implements Serializable{
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
...
}
正如您在查看数据库时看到的那样,在表 cart_item (子对象)中,字段 cart_id 具有指向表 cart (父对象)的字段 id 的外键。
这是我保存对象的方式:
- 有一个读取 JSON 对象的 restController :
@RestController
@RequestMapping(value = "rest/cart")
public class CartRestController {
@Autowired
private CartService cartService;
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.CREATED)
public void create(@RequestBody CartDto cartDto) {
cartService.create(cartDto);
}
}
- 这是 CartService ,它只是一个 接口:
public interface CartService {
void create(CartDto cartDto);
}
这是 CartService 的实现:
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CartServiceImpl implements CartService {
@Autowired
private CartDao cartDao;
@Override
public void create(CartDto cartDto) {
cartDao.create(cartDto);
}
}
CartDao 只是另一个接口,我只向您展示它的实现:
@Repository
public class CartDaoImpl implements CartDao {
@Autowired
private SessionFactory sessionFactory;
// in this method I save the parent and its children
@Override
public void create(CartDto cartDto) {
Cart cart = new Cart();
List<CartItem> cartItems = new ArrayList<>();
cartDto.getCartItems().stream().forEach(cartItemDto ->{
//here I fill the CartItem objects;
CartItem cartItem = new CartItem();
...
cartItem.setCart(cart);
cartItems.add(cartItem);
});
cart.setCartItems(cartItems);
sessionFactory.getCurrentSession().save(cart);
}
}
当我尝试保存新 购物车 及其 cart_item 时 出现此错误:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/webstore] threw
exception [Request processing failed; nested exception is
org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of
class
[com.depasmatte.webstore.domain.CartItem] with identifier [7]: optimistic locking failed;
nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by
another transaction (or unsaved-value mapping was incorrect) :
[com.depasmatte.webstore.domain.CartItem#7]] with root cause
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction
(or unsaved-value mapping was incorrect) : [com.depasmatte.webstore.domain.CartItem#7]
我想错误取决于这样一个事实,即当 Hibernate 尝试保存 cart_item 时, 购物车 的 ID 尚不存在!
在镜头中保存父对象及其子对象的正确方法是什么?谢谢
原文由 MDP 发布,翻译遵循 CC BY-SA 4.0 许可协议
以下是您应该遵循 的规则列表,以便能够一次性存储父实体及其子实体:
PERSIST
应该启用(CascadeType.ALL
也可以)映射问题:
@Column(name="id")
cartItems
制作 setter --- private 。由于 Hibernate 使用它自己的List
的实现,你永远不应该通过 setter 直接改变它private List<CartItem> cartItems = new ArrayList<>();
@ManyToOne(optional = false)
代替nullable = false
在@JoinColumn
fetch = FetchType.LAZY
收藏Cart
应该有一个方法:设计问题:
save
这样的样板