[英]What is the right way to create custom query in spring-data-jpa
I am facing an issue where when I run my spring boot application with my custom query method, I am getting an exception.我遇到了一个问题,当我使用自定义查询方法运行 spring 引导应用程序时,出现异常。
My repository class is below我的存储库 class 在下面
public interface ProductRepository extends Repository<Product,Integer> {
@Query(name = "SELECT p.rate FROM Product p WHERE p.id=:id")
public Optional<String> findRateById(@Param("id") int id);
@Modifying
@Query(name = "UPDATE Product p set p.productName=:productName WHERE p.id=:id")
public int updateProductNameById(@Param("id") int id,@Param("productName") String
productName);
}
And the exception that I'm getting我得到的例外
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract int com.spring.data.jpa.repository.ProductRepository.updateProductNameById(int,java.lang.String)! No property updateProductNameById found for type Product!
The name property within the @Query annotation determines the use of a NamedQuery. @Query 注解中的 name 属性决定了 NamedQuery 的使用。 If you want to use Named Query than you should use the @NamedQuery annotation in your entity.
如果要使用命名查询,则应在实体中使用 @NamedQuery 注释。
@Entity
@Table
@NamedQuery(name="updateProduct", query="UPDATE PRODUCT ....")
public class Product
You can read more about it here JPA你可以在这里阅读更多关于它的信息JPA
If you don't want to use namedquery than, just remove the name property inside the @Query annotation.如果您不想使用namedquery,只需删除@Query 注释内的name 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.