如何解决如何在同一个类中创建一对一映射和一对多
我正在创建一个在线购物网站。它有三个简单的表,分别是cart、cartItem 和shipping details。我使用 spring boot 和 MysqL 来开发这个。我以某种方式设法在 Cart 和 CartItem 之间进行一对多映射。这行得通。我可以保存数据,也可以从数据库中获取数据。 但是当我添加运输详细信息时它不起作用
那些是我创建的表
create table cart (
id bigint not null auto_increment,cart_date date,fk_cus_id integer not null,item_count integer not null,total_price double precision not null,primary key (id)
) engine=InnoDB
create table cart_item (
id bigint not null auto_increment,paid bit not null,product_id integer not null,product_name varchar(255),qty integer not null,seller_id integer not null,status integer not null,cart_cart_id bigint,primary key (id)
) engine=InnoDB
create table shipping (
id bigint not null auto_increment,address_line_one varchar(255),city_code integer not null,district_code integer not null,f_name varchar(255),l_name varchar(255),province_code integer not null,primary key (id)
) engine=InnoDB
alter table cart_item
add constraint FK7vsju2wftw7285wp7maxhi2pd
foreign key (cart_cart_id)
references cart (id)
alter table shipping
add constraint FKdvyy28kroew5djqmb6739f89w
foreign key (cart_cart_id)
references cart (id)
那些是我创建的模型类
@Entity
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int productId;
private int sellerId ;
private String productName;
private int qty;
private double totalPrice;
private int status;
private boolean paid;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private Cart cart;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public int getSellerId() {
return sellerId;
}
public void setSellerId(int sellerId) {
this.sellerId = sellerId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public boolean isPaid() {
return paid;
}
public void setPaid(boolean paid) {
this.paid = paid;
}
}
@Entity
public class Cart implements Serializable {
private static final long serialVersionUID = 8155938349165825920L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private double totalPrice;
private int fkCusId;
private Date cartDate;
private int itemCount ;
@OnetoMany(mappedBy = "cart",cascade = CascadeType.ALL,orphanRemoval = true)
private List<CartItem> cartItems;
/* @OnetoOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cart_cart_id",referencedColumnName = "id")*/
@OnetoOne(mappedBy = "cart",cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Shipping shippingDetails;
public void setCart(CartItem cartItem) {
cartItem.setCart(this);
shippingDetails.setCart(this);
}
public Shipping getShippingDetails() {
return shippingDetails;
}
public void setShippingDetails(Shipping shippingDetails) {
this.shippingDetails = shippingDetails;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public int getFkCusId() {
return fkCusId;
}
public void setFkCusId(int fkCusId) {
this.fkCusId = fkCusId;
}
public Date getCartDate() {
return cartDate;
}
public void setCartDate(Date cartDate) {
this.cartDate = cartDate;
}
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
public List<CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(List<CartItem> cartItems) {
this.cartItems = cartItems;
}
}
@Entity
public class Shipping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int fkCusId;
private String fName;
private String lName;
private String addressLineOne;
private int cityCode;
private int provinceCode;
private int districtCode;
@OnetoOne
@MapsId
@JoinColumn(name = "id")
private Cart cart;
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getFkCusId() {
return fkCusId;
}
public void setFkCusId(int fkCusId) {
this.fkCusId = fkCusId;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getAddressLineOne() {
return addressLineOne;
}
public void setAddressLineOne(String addressLineOne) {
this.addressLineOne = addressLineOne;
}
public int getCityCode() {
return cityCode;
}
public void setCityCode(int cityCode) {
this.cityCode = cityCode;
}
public int getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(int provinceCode) {
this.provinceCode = provinceCode;
}
public int getdistrictCode() {
return districtCode;
}
public void setdistrictCode(int districtCode) {
this.districtCode = districtCode;
}
}
这是服务类。所有存储库都使用 crudRepository 扩展
@Service
public class CartService {
@Autowired
CartItemRepository cartItemRepository;
@Autowired
Cartrepository cartrepository;
@Autowired
ShippingRepository shippingRepository;
public Cart addProductToCart(Cart cart){
cart.getCartItems().forEach(item -> cart.setCart(item));
return cartrepository.save(cart);
}
}
这是 JSON 负载
{
"totalPrice":152500,"fkCusId":1,"cartDate":"2020-12-03","itemCount":1,"cartItems":[{
"productId":1,"sellerId":1,"productName":"acer lap","qty":1,"totalPrice":152500
}],"shippingDetails":{
"fkCusId":2,"fName":"Joe","lName":"Bidan","addressLineOne":"testing","cityCode":1,"provinceCode":1,"districtCode":1
}
}
系统运行良好,无需运输详细信息。当我将运输详细信息作为属性添加到购物车时 一对一映射,不起作用。有人可以给我一个代码示例,说明如何将运输详细信息添加到购物车并正确映射
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。