提问人:BHeiB 提问时间:10/28/2022 更新时间:10/28/2022 访问量:291
Spring Boot 查询或休眠验证问题参数值 [1] 与预期类型 [java.lang.Integer (n/a)] 不匹配
Spring Boot Query or Hibernate problem with validation Parameter value [1] did not match expected type [java.lang.Integer (n/a)]
问:
我正在为我的大学写一个小项目,但我遇到了一个问题。我想了 5 天,仍然不知道我做错了什么。
Parameter value [1] did not match expected type [java.lang.Integer (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Integer (n/a)]
at com.project.warehouse.service.impl.CartServiceImpl.addCartbyUserIdAndItemId(CartServiceImpl.java:54)
at com.project.warehouse.controller.AddToCartController.addCartWithItem(AddToCartController.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by: java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Integer (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54)
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27)
at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90)
@Query("Select addCart FROM AddToCart addCart WHERE addCart.user_id=:user_id and addCart.item.id=:item_id")
Optional<AddToCart> getCartByProductIdAnduserId(@Param("item_id")Long item_id, @Param("user_id")Long user_id);
@Override
public List<AddToCart> addCartbyUserIdAndItemId(long itemId, long userId, int quantity, double price) throws Exception {
try {
if(addToCartRepository.getCartByProductIdAnduserId(userId, itemId).isPresent()) {
throw new Exception("Product is already exist.");
}
AddToCart obj = new AddToCart();
obj.setQuantity(quantity);
obj.setUser_id(userId);
Item item = itemService.getItemById(itemId);
obj.setItem(item);
//TODO price has to check with qty
obj.setPrice(price);
addToCartRepository.save(obj);
return this.getCartByUserId(userId);
} catch (Exception e) {
e.printStackTrace();
logger.error("" + e .getMessage());
throw new Exception(e.getMessage());
}
}
@Entity
@Table(name = "item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
private int quantity;
private double price;
private boolean inStock;
public Integer getItemId() {
return id;
}
public void setItemId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isInStock() {
return inStock;
}
public void setInStock(boolean inStock) {
this.inStock = inStock;
}
我检查了每个具有类似主题的帖子。查询看起来不错,我通过前端(反应)发送请求,Postman也得到了同样的请求。也许你会知道这里发生了什么。Hibernate 版本是 5.6.8 final
答: 暂无答案
评论