我需要使用其 ID 洗掉物體,嘗試使用 JPA 存盤庫 delete by id 方法
productRepository.deleteById(id);
其中“id”應該是 Long-如果傳遞的字串值將給出錯誤:Long expected
Sample id= "402880f17ebd7e44017ebd7fe9ee0000"
因此我無法將字串轉換為 Long
物體類
@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>{
}
uj5u.com熱心網友回復:
在您的 productRepository 中,將介面上的第二個泛型型別從 long 更改為 string。所以改變你的代碼片段
@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {}
到:
@Repository
public interface ProductRepository extends JpaRepository<Product,String> {}
此外,您不需要@RepositorySpring 資料 JPA 介面。
uj5u.com熱心網友回復:
在您的 ProductRepository 中,當您擴展JpaRepository<EntityName,EntityId Type>.
您的情況時,您使用欄位 id 作為字串,但將型別擴展為 Long。要解決您的問題,請以 Long 數字形式輸入或將主鍵型別轉換為 String。
uj5u.com熱心網友回復:
主鍵型別錯誤,應該是'String',而不是'Long':
@Repository
public interface ProductRepository extends JpaRepository<Product,String>{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424548.html
