如何使用 @Query 注解

huangapple go评论93阅读模式
英文:

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&lt;Organization, Long&gt; {
        
    List&lt;Organization&gt; findByDeleted(Boolean isDeleted);
    
    List&lt;Organization&gt; findAllByDeletedOrderById(Boolean isDeleted);

    @Query(value=&quot;SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 &quot;, nativeQuery = true)
    Organization findFirstNameByNameLike(@Param(&quot;organizationId&quot;) Long organizationId, @Param(&quot;name&quot;) 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 &#39;Optional[name]&#39; not found in annotated query &#39;SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:organizationId% LIMIT 1 &#39;!

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=&quot;SELECT * FROM ORGANIZATION WHERE id = :organizationId AND NAME LIKE %:name% LIMIT 1 &quot;, nativeQuery = true)

huangapple
  • 本文由 发表于 2020年8月25日 06:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63569550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定