spring框架中的多对多关系

时间:2015-11-17 18:35:56

标签: java jpa

目前我正在使用Spring Framwork应用程序来试用它的工作原理。 我遇到了多对多关系并将数据插入其中的问题。

例如,我通过链接表制作包含一个或多个商品的订单。

我想一次存储一个订单,包括与其相关的所有产品(产品已经存在)。问题是当我尝试存储数据时,orderId没有存储在product_order表中,并且给出了以下错误:

""列' ORDER_ID'不能为空""。

有没有人知道我做错了什么?或者,如果这种方式不可能。 SQL结构& Java代码就在最后。

我尝试存储数据的方式是:

curl -i -X POST -H "Content-Type:application/json" -d '{
  "code" : "Test",
  "statusId" : 2,
  "userId" : 1,
  "productOrders" : [ {
    "price" : 1.0,
    "amount" : 2,
    "product" : {
      "productId" : 1
    }
  }, {
    "price" : 2.0,
    "amount" : 3,
    "product" : {
      "productId" : 2
    }
  } ]
}' http://localhost:8080/order

这是我使用的SQL结构:

CREATE TABLE `user` (
  `USER_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `USER_NAME` VARCHAR(20) NOT NULL,
  `USER_BALANCE` DOUBLE UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`USER_ID`)
);


CREATE TABLE `product` (
  `PRODUCT_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `PRODUCT_NAME` VARCHAR(20) NOT NULL,
  `PRODUCT_PRICE` DOUBLE UNSIGNED NOT NULL,
  `PRODUCT_DESCRIPTION` VARCHAR(45),
  PRIMARY KEY (`PRODUCT_ID`)
);


CREATE TABLE  `order` (
  `ORDER_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `ORDER_USER_ID` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY (`ORDER_ID`),
  CONSTRAINT `FK_ORDER_USER_ID` FOREIGN KEY (`ORDER_USER_ID`)
               REFERENCES `user` (`USER_ID`)
);


CREATE TABLE  `product_order` (
  `ORDER_ID` INT(10) UNSIGNED NOT NULL,
  `PRODUCT_ID` INT(10) UNSIGNED NOT NULL,
  `AMOUNT` INT(10) UNSIGNED NOT NULL,
  `PRICE` DOUBLE NOT NULL,
  PRIMARY KEY (`ORDER_ID`,`PRODUCT_ID`),
  CONSTRAINT `FK_PRODUCT_ORDER_ORDER_ID` FOREIGN KEY (`ORDER_ID`)
             REFERENCES `order` (`ORDER_ID`),
  CONSTRAINT `FK_PRODUCT_ORDER_PRODUCT_ID` FOREIGN KEY (`PRODUCT_ID`)
             REFERENCES `product` (`PRODUCT_ID`)
);

这是我的课程(删除了getter / setter,留下了注释)

@Entity
@Table(name = "product")
public class Product implements java.io.Serializable {

    private Integer productId;
    private String name;
    private String description;
    private Double price;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "PRODUCT_ID", unique = true, nullable = false)

    @Column(name = "PRODUCT_NAME", nullable = false)

    @Column(name = "PRODUCT_PRICE", nullable = false)

    @Column(name = "PRODUCT_DESCRIPTION", nullable = false)
}

@Entity
@Table(name = "`order`")
public class Order implements java.io.Serializable {
    private Integer orderId;
    private Integer userId;
    private Set<ProductOrder> productOrders = new HashSet<>(0);

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ORDER_ID", unique = true, nullable = false)

    @Column(name = "ORDER_USER_ID")

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.order", cascade = CascadeType.ALL)
}


@Entity
@Table(name = "product_order")
@AssociationOverrides({
        @AssociationOverride(name = "pk.order",
                joinColumns = @JoinColumn(name = "ORDER_ID")),
        @AssociationOverride(name = "pk.product",
                joinColumns = @JoinColumn(name = "PRODUCT_ID")) })
public class ProductOrder implements java.io.Serializable {
    private ProductOrderId pk = new ProductOrderId();
    private Double price;
    private Integer amount;

    @EmbeddedId
    //Getter/setter for primaryKey (pk)

    @Transient
    //Link to product

    @Column(name = "PRICE")

    @Column(name = "AMOUNT")
}

@Embeddable
public class ProductOrderId implements java.io.Serializable {
    private Product product;
    private Order order;

    @ManyToOne
    //link to Product

    @ManyToOne
    //Link to Order
}

0 个答案:

没有答案