Java实体java.sql.SQLException:字段“ id”没有默认值

时间:2019-02-25 10:53:55

标签: java spring curl

我使用的API希望将产品ID作为字符串并自行提供。提供了模型类,

@Entity
public class Product{


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

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

    @Embedded
    private Stock stock;

    public Product() {

    }

    public Product(String p_id, Timestamp p_timestamp, Stock stock) {
        this.p_id = p_id;
        this.p_timestamp = p_timestamp;
        this.stock = stock;
    }

    public String getP_id() {
        return p_id;
    }

    public void setP_id(String p_id) {
        this.p_id = p_id;
    }

    public Timestamp getP_timestamp() {
        return p_timestamp;
    }

    public void setP_timestamp(Timestamp p_timestamp) {
        this.p_timestamp = p_timestamp;
    }

    public Stock getStock() {
        return stock;
    }

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




import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.sql.Timestamp;


@Embeddable
public class Stock {

    @Column(name = "s_id")
    private String s_id;

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

    @Column(name = "s_s_quantity")
    private int s_quantity;

    public Stock() {

    }

    public Stock(String s_id, Timestamp s_timestamp, int s_quantity) {

        this.s_id = s_id;
        this.s_timestamp = s_timestamp;
        this.s_quantity = s_quantity;
    }

    public String getS_id() {
        return s_id;
    }

    public void setS_id(String s_id) {
        this.s_id = s_id;
    }

    public Timestamp getS_timestamp() {
        return s_timestamp;
    }

    public void setS_timestamp(Timestamp s_timestamp) {
        this.s_timestamp = s_timestamp;
    }

    public int getS_quantity() {
        return s_quantity;
    }

    public void setS_quantity(int s_quantity) {
        this.s_quantity = s_quantity;
    }
}

MySQL数据库在这里

enter image description here

我通过终端传递了cURL命令,

$ curl -i -X POST -H "Content-Type:application/json" -d "{  \"p_id\": \"Product ID\", \"p_timestamp\": \"2017-07-16 22:54:01.754\",  \"stock\" : {  \"s_id\": \"Stock ID\", \"s_timestamp\": \"2000-07-16 22:54:01.754\",  \"s_quantity\": \"250\"  }}" http://localhost:8080/api/v1/products/createProduct

我收到错误消息,

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]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1109) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1057) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1377) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1042) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3171) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3686) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3283) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2479) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]

如何设置ID的默认值?谢谢。

4 个答案:

答案 0 :(得分:1)

从数据库中删除id字段

答案 1 :(得分:0)

由于它是您的ID,因此可以使用@GeneratedValue生成它:

@Id
@GeneratedValue
@Column(name = "p_id")
private String p_id;

这样,Hibernate将为您处理ID。

答案 2 :(得分:0)

您的实体没有公开名为id的字段,但是您的表将其中一个字段定义为不可为空。

如果由于使用id而不需要p_id,只需从表中删除该列,否则就需要在实体中添加一个引用名为{{1 }},然后在您的插入内容中为其赋值,或在表格中设置默认值。

答案 3 :(得分:0)

  • 插入记录时,您必须为'p_id'列传递值。
  • 'p_id'列是主键,因此它不是null。

  • 您可以为此创建自定义自动递增逻辑,也许link可以帮助您