0

I work with an API where I would like to have the Product ID as the String and supply myself. The model classes are provided,

@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;
    }
}

The MySQL database is here,

enter image description here

I pass the cURL command via the terminal,

$ 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

I get the error message,

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]

How do I set the default value for the id? Thank you.

Arefe
  • 11,321
  • 18
  • 114
  • 168
  • If 'id' field can not be nullable you should set default value . – nissim abehcera Feb 25 '19 at 10:57
  • @nissimabehcera I think I dont need the id field – Arefe Feb 25 '19 at 10:58
  • Can't you make the id anything like `PRIMARY KEY AUTO INCREMENT`? It looks like those ids should be managed by the database engine. – deHaar Feb 25 '19 at 11:01
  • Ok, the question would be how to do that for a String field? – Arefe Feb 25 '19 at 11:03
  • @Arefe If you are using hibernate it needs a primary key. You should have something like auto increment enabled on the id field at the database level or set id with some Sequence Generators \@Id \@GeneratedValue(strategy = GenerationType.SEQUENCE) \@Column(name = "id", updatable = false, nullable = false) private Long id; – Santosh b Feb 25 '19 at 11:06
  • https://thoughts-on-java.org/jpa-generate-primary-keys/ – Santosh b Feb 25 '19 at 11:07

3 Answers3

1

Remove the id field from the database

Azad
  • 161
  • 1
  • 6
0

Your entity doesn't expose a field named id, but your table defines one as not nullable.

If you don't need that id because your using p_id, just remove that column from your table, otherwise you need to add a field in your entity referencing to a column named id and either give it a value in your insert or set a default in your table.

noiaverbale
  • 1,550
  • 13
  • 27
0
  • You must have to pass a value for the 'p_id' column while inserting a record.
  • 'p_id' column is a primary key, So it is not null.

  • you can create your custom autoincrement logic for that, maybe this link help you