@Query注解的使用方法
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 '!
如有任何帮助,我将不胜感激
您两次使用了 organizationId 而不是名称。像这样:
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:name% LIMIT 1 ", nativeQuery = true)
我正在尝试使用 @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 '!
如有任何帮助,我将不胜感激
您两次使用了 organizationId 而不是名称。像这样:
@Query(value="SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:name% LIMIT 1 ", nativeQuery = true)