英文:
How to use @Query annotation
问题
我正尝试使用 @Query 在我的 JPA 存储库中创建一个自定义方法,但在运行时遇到以下错误,请帮我检查一下。提前感谢。
以下是代码:
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
List<Organization> findByDeleted(Boolean isDeleted);
List<Organization> findAllByDeletedOrderById(Boolean isDeleted);
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 ", nativeQuery = true)
Organization findFirstNameByNameLike(@Param("organizationId") Long organizationId, @Param("name") String name);
}
以下是运行时错误:
Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract com.oasis.firsbacklogbackend.entity.Organization com.oasis.firsbacklogbackend.repository.OrganizationRepository.findFirstNameByNameLike(java.lang.Long,java.lang.String) but parameter 'Optional[name]' not found in annotated query 'SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 '!
我会非常感谢任何帮助。
英文:
I am trying to use the @Query to create a custom method in my jpa repository but I am getting the following error at runtime, please help me check it out. Thanks in advance
below is the code
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
List<Organization> findByDeleted(Boolean isDeleted);
List<Organization> findAllByDeletedOrderById(Boolean isDeleted);
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 ", nativeQuery = true)
Organization findFirstNameByNameLike(@Param("organizationId") Long organizationId, @Param("name") String name);
}
below is the runtime error
Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract com.oasis.firsbacklogbackend.entity.Organization com.oasis.firsbacklogbackend.repository.OrganizationRepository.findFirstNameByNameLike(java.lang.Long,java.lang.String) but parameter 'Optional[name]' not found in annotated query 'SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 '!
I would appreciate any help
答案1
得分: 1
你两次使用了 organizationId,而不是 name。应该像这样修改:
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:name% LIMIT 1 ", nativeQuery = true)
英文:
You used organizationId twice instead of name. Like this instead:
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:name% LIMIT 1 ", nativeQuery = true)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论