我使用一个小的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
,否则会出现错误。不知道为什么股票编号不存在。如果需要,我可以提供回购和服务代码。
如何将数据正确保留在MySQL中?
更新
当我从Stock中删除@Id注释并将其重命名为id1时,我能够将值持久保存到数据库中。我仍然无法将产品的时间戳保存到数据库中。
答案 0 :(得分:1)
我所缺少的是,为什么首先要处理Product
和Stock
?
Product
和Stock
使用完全相同的表示,为什么要嵌入Stock
?
但是无论如何。您正在嵌入,这意味着这些列将被视为同一数据库表的一部分。
您要传递两个不同的id
和"Product1 ID"
。您还将同一列"Stock ID"
映射到两个不同的字段id
和Product#id
,我认为JPA不应该处理这些字段。
您还要指定两次Stock#id
注释,这将不起作用。