I have written a rest controller that receives a request and saves the data in db. This is my post request that I am trying to send using postman.
{
"product_id" : "testproduct",
"default_concurrent":10,
"default_connections":1000,
"msan":10
}
My entity class :
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Entity
@Table(name="products")
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id
@Column(name="product_id", nullable = false)
private String product_id;
@Column(name="default_concurrent", nullable = true)
private int default_concurrent;
@Column(name="default_connections", nullable = true)
private int default_connections;
@Column(name="msan", nullable = true)
private int msan;
public String getProduct() {
return product_id;
}
public void setProduct(String product) {
this.product_id = product;
}
public int getDefault_concurrent() {
return default_concurrent;
}
public void setDefault_concurrent(int default_concurrent) {
this.default_concurrent = default_concurrent;
}
public int getDefault_connections() {
return default_connections;
}
public void setDefault_connections(int default_connections) {
this.default_connections = default_connections;
}
public int getMsan() {
return msan;
}
public void setMsan(int msan) {
this.msan = msan;
}
}
My Controller :
@PostMapping("/add")
public Product addProduct(@Valid @RequestBody Product product)
{
return productDao.saveProduct(product);
}
Error:
ids for this class must be manually assigned before calling save()
nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.test.model.Product",
I have gone through a lot of posts related to this error on SO, and most of them suggest to use
@GeneratedValue(strategy=GenerationType.AUTO) or @GeneratedValue(strategy=GenerationType.IDENTITY)
but I cant use it as my primary key is going to be string and I cant change it to Long.
While debugging I can see that in request body I am receiving product_id as null. Remaining attributes are having correct values.
But if I create one more column as 'Id' in the DB and update the Entity class accordingly making 'Id' as primary key and try to execute then I am getting the correct values in request body and it is getting saved in the DB as well.
Need help.