Java 为什么我在控制器中收到来自邮递员请求的null?
我已经编写了一个rest控制器,它接收请求并将数据保存在db中。 这是我的post请求,我正试图使用邮递员发送该请求Java 为什么我在控制器中收到来自邮递员请求的null?,java,hibernate,rest,spring-boot,jpa-2.0,Java,Hibernate,Rest,Spring Boot,Jpa 2.0,我已经编写了一个rest控制器,它接收请求并将数据保存在db中。 这是我的post请求,我正试图使用邮递员发送该请求 { "product_id" : "testproduct", "default_concurrent":10, "default_connections":1000, "msan":10 } 我的实体类: import javax.persistence.Column; import javax.persistence.Entity; impo
{
"product_id" : "testproduct",
"default_concurrent":10,
"default_connections":1000,
"msan":10
}
我的实体类:
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;
}
}
@PostMapping("/add")
public Product addProduct(@Valid @RequestBody Product product)
{
return productDao.saveProduct(product);
}
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",
我的控制器:
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;
}
}
@PostMapping("/add")
public Product addProduct(@Valid @RequestBody Product product)
{
return productDao.saveProduct(product);
}
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",
错误:
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;
}
}
@PostMapping("/add")
public Product addProduct(@Valid @RequestBody Product product)
{
return productDao.saveProduct(product);
}
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",
我已经阅读了很多关于这个错误的帖子,大多数都建议使用
@GeneratedValue(策略=GenerationType.AUTO)或
@GeneratedValue(策略=GenerationType.IDENTITY)
但我不能使用它,因为我的主键是字符串,我不能将其更改为长
在调试时,我可以看到在请求体中,我接收到的产品标识为null。其余属性具有正确的值
但是,如果我在DB中创建一个以上的列作为'Id',并相应地更新实体类,将'Id'作为主键并尝试执行,那么我将在请求体中获得正确的值,它也将保存在DB中
我需要帮助
因为我们将下划线字符视为保留字符,所以
强烈建议遵循标准Java命名约定(即,
在属性名称中不使用下划线,而是使用大小写)
下划线是Spring数据查询派生中的一个保留字符,可能允许手动属性路径描述
坚持使用成员变量名的Java命名约定,一切都将按预期工作
在使用JPA特性时,您还将面临一个问题。因此,我建议更改命名约定。
改变
对,
更新1 为什么产品标识仅为空
我在IDE中粘贴了相同的代码,发现实体Product缺少字段Product_id的getter setter。添加它后,相同的代码将值保存到数据库中。但我仍然建议大家避免该字段的uu-in-names,以避免进一步的问题,并按照doc遵循命名约定。当使用JPA时,例如findBy(实体中名称为uu的东西),您可能会遇到类似于的问题,如果您的模型中应该是getProduct_id和setProduct_id。但是更好的是,您应该改为使用javatry中的camelcase,使用
@JsonProperty
,而不是使用\u
作为变量名称感谢Patel Romil,我完全不知道这个事实!!还有一个疑问,我只为product_id设置了null,但对于其他字段(如default_connections和default_concurrent),我没有设置null,尽管它们也有下划线?Hello@NaveenYadav,我已将您的代码粘贴到IDE中,发现您的产品模型缺少product_id的getter setter。如果没有getter setter,它产生了与上面相同的问题,但在我添加此内容后,我可以将其保存到database@NaveenYadav但我仍然建议您在使用JPA(如findBy(某物))时避免在字段名中使用uu您可能会面临类似于OK的问题,现在我得到了问题……最后我可以得出结论,下划线不是真正的罪魁祸首……是setter方法必须是“setProduct_id”,而不是@Jerry06建议的“setProduct”,因为我想在内部它将寻找以前的类型setter。谢谢