java - Spring 启动 java.sql.SQLException : Field 'id' doesn't have a default value

标签 java spring-boot http curl response

我在 Java 和 Spring Boot 项目中工作,我打算在其中进行基本设置。对单个对象的 GET 请求应该像这样返回 JSON,

 {
   "productId": “string", // id of the requested product, e.g. "vegetable-123" 
  "requestTimestamp": “dateTime", // datetime in UTC when requested the stock 

  "stock": {

     "id": "string", 
     "timestamp": 
     "dateTime" "quantity": "integer"

   } 
}

我制作提供的模型,

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name ="id")
    private String id;

    @Column(insertable = false, updatable = false)
    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;
    }
}



@Embeddable
public class Stock {

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

    @Column(name = "s_timestamp")
    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;
    }
}

存储库如下,

@Repository
public interface ProductRepository extends CrudRepository<Product, String>{

}

服务类在这里,

@Service
public class ProductService {

    @Autowired
    private ProductRepository repository;

    public Optional<Product> findById(String id) {
        return repository.findById(id);
    }

    public List<Product> findAll() {
        return (List<Product>) repository.findAll();
    }

    public Product save(Product product) {
        return repository.save(product);
    }
}

我尝试使用以下 API 请求发布产品,

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


    @Autowired
    private ProductService service;



    @PostMapping(value = "/createProduct", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Product> createProduct(@RequestBody Product product) {

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

}

我通过 cURL 执行它,

$ curl -i -X POST -H "Content-Type:application/json" -d "{\"id\" : \"Apple Inc\",\"timestamp\" : \"2017-07-16T22:54:01.754Z\",\"stockId\" : \"Apple Stock\", \"s_timestamp\": \"2018-07-16T22:54:01.754Z\", \"quantity\": \"115\"}" http://localhost:8080/api/v1/products/createProduct

我立即通过终端收到响应,

HTTP/1.1 500 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 14 Feb 2019 14:04:20 GMT
Connection: close

{"timestamp":"2019-02-14T14:04:20.482+0000","status":500,"error":"Internal Server Error","message":"could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement","path":"/api/v1/products/createProduct"}

IDE 正在显示消息,

java.sql.SQLException: Field 'id' doesn't have a default value
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.15.jar:8.0.15]

如何为 id 提供默认值?

最佳答案

这是一个错误:

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name ="id")
private String id;

JPA 无法以这种方式生成 String 主键 Info .

尝试这样的事情:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "id")
private String id;

关于java - Spring 启动 java.sql.SQLException : Field 'id' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692673/

相关文章:

java - 使用 gzip 压缩 InputStream

java - JIT 编译器可以内联 Java 8 默认接口(interface)方法吗?

java正则表达式不能使用符号?和 {n} 一起

java - Windows(7?)上 Java 7 命令行的损坏的通配符扩展

node.js - 如何使用 Express API 发送 10x 响应?

java - java中合并两个对象

java - 当 HikariCP 调试日志的连接总数为 2 时,Spring boot 指标显示 HikariCP 连接创建计数为 1

spring - 第三方健康端点最佳实践

python - Flask Python 中的 HTTP 状态 405 和 Restful API

java - 在 java : HTTP response code 414 中通过 http 读取文件