验证方法 public 的查询失败,Spring JPA?

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

Validation failed for query for method public, spring jpa?

问题

我正在创建一个用于从数据库查询并返回结果的API。

以下是请求的部分代码:

  1. @RequestMapping(value = "/config", params = { "appCode", "appVersion" }, method = RequestMethod.GET)
  2. public List<AppConfig> getConfig(@RequestParam(value = "appCode", required = true) String appCode,
  3. @RequestParam(value = "appVersion", required = true) String appVersion) {
  4. return configRepository.findByCodeAndVersion(appCode, appCode);
  5. }

表类的部分代码:

  1. @Entity
  2. @Table(name = "app_config")
  3. @EntityListeners(AuditingEntityListener.class)
  4. public class AppConfig {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column(columnDefinition = "serial")
  8. private long id;
  9. @Column(name = "app_code", nullable = false)
  10. private String appCode;
  11. @Column(name = "app_name", nullable = false)
  12. private String appName;
  13. @Column(name = "api_url", nullable = true)
  14. private String apiUrl;
  15. @Column(name = "db_name", nullable = true)
  16. private String dbName;
  17. @Column(name = "app_version", nullable = false)
  18. private String appVersion;
  19. }

包含自定义查询的存储库部分代码:

  1. @Repository
  2. public interface AppConfigRepository extends CrudRepository<AppConfig, Long> {
  3. @Query("SELECT n FROM AppConfig WHERE n.appCode = ?1 and n.appVersion = ?2")
  4. List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);
  5. }

在运行应用程序时,我遇到了以下异常:

  1. Validation failed for query for method public abstract java.util.List com.api.repository.AppConfigRepository.findByCodeAndVersion(java.lang.String,java.lang.String)!
英文:

I am creating an api to query from the DB and return the result.

This is the request

  1. @RequestMapping(value = &quot;/config&quot;, params = { &quot;appCode&quot;, &quot;appVersion&quot; }, method = RequestMethod.GET)
  2. public List&lt;AppConfig&gt; getConfig(@RequestParam(value = &quot;appCode&quot;, required = true) String appCode,
  3. @RequestParam(value = &quot;appVersion&quot;, required = true) String appVersion) {
  4. return configRepository.findByCodeAndVersion(appCode, appCode);
  5. }

The table class

  1. @Entity
  2. @Table(name = &quot;app_config&quot;)
  3. @EntityListeners(AuditingEntityListener.class)
  4. public class AppConfig {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column(columnDefinition = &quot;serial&quot;)
  8. private long id;
  9. @Column(name = &quot;app_code&quot;, nullable = false)
  10. private String appCode;
  11. @Column(name = &quot;app_name&quot;, nullable = false)
  12. private String appName;
  13. @Column(name = &quot;api_url&quot;, nullable = true)
  14. private String apiUrl;
  15. @Column(name = &quot;db_name&quot;, nullable = true)
  16. private String dbName;
  17. @Column(name = &quot;app_version&quot;, nullable = false)
  18. private String appVersion;
  19. }

The repository where I am having custom query

  1. @Repository
  2. public interface AppConfigRepository extends CrudRepository&lt;AppConfig, Long&gt; {
  3. @Query(&quot;SELECT n FROM AppConfig WHERE n.appCode = ?1 and n.appVersion = ?2&quot;)
  4. List&lt;AppConfig&gt; findByCodeAndVersion(String appCode, String appVersion);
  5. }

On running the application I get the exception

  1. Validation failed for query for method public abstract java.util.List com.api.repository.AppConfigRepository.findByCodeAndVersion(java.lang.String,java.lang.String)!

答案1

得分: 1

你需要在查询中的实体名称 AppConfig 后面添加别名 n,应该像这样:

  1. @Query("SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2")
  2. List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);

你也可以在查询字符串内部使用命名参数,就像这样:

  1. @Query("SELECT n FROM AppConfig n WHERE n.appCode = :appCode and n.appVersion = :appVersion")
  2. List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);

像这样的查询可以通过 Spring 数据查询方法处理,只需确保将方法重命名为实体的字段名称:

  1. List<AppConfig> findByAppCodeAndAppVersion(String appCode, String appVersion);
英文:

You have to add the alias n after the entity name AppConfig in the query, it should be like:

  1. @Query(&quot;SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2&quot;)
  2. List&lt;AppConfig&gt; findByCodeAndVersion(String appCode, String appVersion);

You can also use named parameter inside the query string like this:

  1. @Query(&quot;SELECT n FROM AppConfig n WHERE n.appCode = :appCode and n.appVersion = :appVersion&quot;)
  2. List&lt;AppConfig&gt; findByCodeAndVersion(String appCode, String appVersion);

And a query like this can be handled by Spring data query methods, just make sure to rename the method to use the field names of the entity:

  1. List&lt;AppConfig&gt; findByAppCodeAndAppVersion(String appCode, String appVersion);

答案2

得分: 0

尝试这个:

  1. @Query("SELECT n FROM AppConfig n WHERE n.appCode = :x and n.appVersion = :y")
  2. List<AppConfig> findByCodeAndVersion(@Param("x") String appCode, @Param("y") String appVersion);
  3. 或者你也可以直接使用这个方法
  4. List<AppConfig> findByAppCodeAndAppVersion(String appCode, String appVersion);
英文:

try this :

  1. @Query(&quot;SELECT FROM AppConfig n WHERE n.appCode = :x and n.appVersion = :y&quot;)
  2. List&lt;AppConfig&gt; findByCodeAndVersion(@Param(&quot;x&quot;)String appCode,@Param(&quot;y&quot;) String appVersion);

or you can use directly the method:

  1. List&lt;AppConfig&gt; findByAppCodeAndAppVersion(String appCode,String appVersion);

huangapple
  • 本文由 发表于 2020年5月30日 14:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098753.html
匿名

发表评论

匿名网友

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

确定