我正在尝试构建构建服务,它用子对象保存对象,但出现了错误。结果对象中的数据字段已保存,但子对象未保存。
我有下一个目标。主对象是订单,子对象是合作伙伴:
@Getter
@Setter
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id")
private int orderId;
@OneToMany(mappedBy = "order", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<Partner> partners;
}
@Getter
@Setter
@Entity
@Table(name = "partners")
public class Partner implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "partner_id")
private int id;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "order_id", nullable = false)
private Order order;
}
我使用spring jpa存储库中的标准嵌入方法“save”:
@Repository
public interface OrdersRepository extends JpaRepository<Order, Integer> {
}
和服务,称之为存储库:
@服务公共类ordersserviceimpl实现ordersservice{
@Autowired
private OrdersRepository repository;
@Override
public Order save(Order order) {
return repository.save(order);
}
}
有人知道为什么伴侣不被拯救吗?
谢谢!
1条答案
按热度按时间rpppsulh1#
因为关系所有者是partner,所以需要先保存订单。或者你可以把cascade=cascadetype.persist放在私有订单上;