【发布时间】:2022-08-12 21:00:27
【问题描述】:
我需要使用其 ID 删除实体,尝试使用 JPA 存储库 delete by id 方法
productRepository.deleteById(id);
其中 \"id\" 应该是 Long- 如果传递的字符串值将给出错误:Long expected
Sample id= \"402880f17ebd7e44017ebd7fe9ee0000\"
因此我不能将字符串转换为长
实体类
@Entity
@Table(name = \"products\")
public class Product {
@Id @GeneratedValue(generator = \"system-uuid\")
@GenericGenerator(name=\"system-uuid\",strategy = \"uuid\")
@Column(name=\"product_id\")
private String id;
@Column(name=\"price\")
@NotNull
private float productPrice;
@Column(name=\"product_name\")
@NotNull
private String productName;
@Column(name=\"description\")
private String productDesc;
@Column(name=\"stock_available\")
private int productStock;
@ManyToOne(optional = false)
@JoinColumn(name = \"shop_id\")
private Shop shop;
public Product(){
}
public Product(String id, float productPrice, String productName, String productDesc, int productStock, Shop shop) {
this.id = id;
this.productPrice = productPrice;
this.productName = productName;
this.productDesc = productDesc;
this.productStock = productStock;
this.shop = shop;
}
}
存储库类:
@Repository
public interface ProductRepository extends JpaRepository<Product,Long>{
}
-
添加有关您的实体和回购的问题详细信息
-
出色地?如何是您应该将该字符串转换为整数值吗?那是十六进制字符串吗?
标签: java spring-boot jpa spring-data-jpa spring-repositories