【问题标题】:Persist all the fields properly in the MySQL在 MySQL 中正确保存所有字段
【发布时间】:2019-07-10 08:00:18
【问题描述】:

我使用一个小型 API,在我将所有数据保存在数据库中之前,我可以在该 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。

对于产品时间戳,我需要使用insertable = false, updatable = false,否则会出错。不知道为什么 Stock id 不存在。如果需要,我可以提供 repo 和服务代码。

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

更新

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

【问题讨论】:

    标签: java mysql rest api spring-boot


    【解决方案1】:

    我想念的是,你为什么首先要处理ProductStockProductStock 使用完全相同的表示,为什么要嵌入 Stock

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

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

    您还指定了两次 @Id 注释,这不起作用。

    【讨论】:

    • 产品更多的是关于公司品牌,让我们把这个问题当作一个编程任务,而不是深入研究它的语义。您的回答很有帮助 - 当我从 Stock 中删除 @Id 注释并将其重命名为 id1 时,我能够将值保存到数据库中。我仍然无法将产品的timestamp 持久化到数据库中。你有什么想法和方法吗?
    • @Arefe 尝试将可插入和可更新的内容移至 Stock#timestamp。并将插入和更新时间戳的责任留给 Product。
    猜你喜欢
    • 2011-10-13
    • 2014-06-03
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2011-07-20
    相关资源
    最近更新 更多