在MySQL中正确保留所有字段

问题描述 投票:0回答:1

我使用一个小API,在我持有数据库之前我拥有所有数据,我可以相应地打印它们。所以,似乎代码按预期工作,但是,我无法将它们保留在MySQL中。提供实体类,

@Entity
public class Product {

    @Id
    @Column(name = "id")
    private String id;


    @Column(insertable = false, updatable = false, name = "timestamp")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private java.sql.Timestamp timestamp;

    @Embedded
    private Stock stock;

    public Product() {

    }

    public Product(String id, Timestamp timestamp, Stock stock) {
        this.id = id;
        this.timestamp = timestamp;
        this.stock = stock;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

    public Stock getStock() {
        return stock;
    }

    public void setStock(Stock stock) {
        this.stock = stock;
    }
}



@Embeddable
public class Stock {

    @Id
    @Column(name = "id")
    private String id;

    @Column(name = "timestamp")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.sql.Timestamp timestamp;

    @Column(name = "quantity")
    private int quantity;

    public Stock() {

    }

    public Stock(String id, Timestamp timestamp, int quantity) {

        this.id = id;
        this.timestamp = timestamp;
        this.quantity = quantity;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

API如下所示,

@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {


    @Autowired
    private ProductService service;


    /*
     *
     * $ curl -i -X POST -H "Content-Type:application/json" -d "{ \"id\": \"Product1 ID\", \"timestamp\": \"2017-07-16 22:54:01.754\", \"stock\" : { \"id\": \"Stock ID\", \"timestamp\": \"3000-07-16 22:54:01.754\", \"quantity\": \"350\" }}" http://localhost:8080/api/v1/products/createProduct
     * */
    @PostMapping(value = "/createProduct", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Product> createProduct(@RequestBody Product product) {


        System.out.println("\n");
        System.out.println("Product ID " + product.getId());
        System.out.println("Product timestamp " +product.getTimestamp());

        System.out.println("Stock ID " +product.getStock().getId());
        System.out.println("Stock timestamp " +product.getStock().getTimestamp());
        System.out.println("Stock quantity " +product.getStock().getQuantity());
        System.out.println("\n");

        service.save(product);
        return ResponseEntity.status(HttpStatus.CREATED).body(product);
    }
}

我对数据库中的持久操作进行了此cURL调用,

$ curl -i -X POST -H "Content-Type:application/json" -d "{ \"id\": \"Product1 ID\", \"timestamp\": \"2017-07-16 22:54:01.754\", \"stock\" : { \"id\": \"Stock ID\", \"timestamp\": \"3000-07-16 22:54:01.754\", \"quantity\": \"350\" }}" http://localhost:8080/api/v1/products/createProduct

我的印刷品说所有数据都是希望的,

Product ID Product1 ID
Product timestamp 2017-07-17 00:54:01.754
Stock ID Stock ID
Stock timestamp 3000-07-17 00:54:01.754
Stock quantity 350

这很好,但是,当我查看MySQL数据库时,我发现缺少产品时间戳和库存ID。

enter image description here

对于产品时间戳,我需要使用insertable = false, updatable = false,否则,我收到错误。不确定为什么Stock id不会持久化。如果需要,我可以提供回购和服务代码。

如何在MySQL中正确保存数据?

UPDATE

当我从Stock中删除@Id注释并将其重命名为id1时,我能够将值保存到数据库中。我仍然无法将产品的时间戳保存到数据库中。

enter image description here

java mysql rest api spring-boot
1个回答
1
投票

我缺少的是,你为什么要首先与ProductStock打交道? ProductStock使用完全相同的表示,为什么嵌入Stock

但无论如何。您正在嵌入,这意味着这些列将被视为同一数据库表的一部分。

而且你传递了两个不同的ids,"Product1 ID""Stock ID"。您还将同一列id映射到两个不同的字段Product#idStock#id,我不认为JPA应该处理它们。

你还指定了@Id注释的两倍,这是行不通的。

© www.soinside.com 2019 - 2024. All rights reserved.