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,
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.
从数据库中删除ID字段
Since it's your id, you can make it generated using @GeneratedValue
:
@Id
@GeneratedValue
@Column(name = "p_id")
private String p_id;
That way Hibernate will handle the ids for you.
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.
'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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.