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

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

Validation failed for query for method public, spring jpa?

问题

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

以下是请求的部分代码:

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

表类的部分代码:

@Entity
@Table(name = "app_config")
@EntityListeners(AuditingEntityListener.class)
public class AppConfig {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private long id;
    @Column(name = "app_code", nullable = false)
    private String appCode;
    @Column(name = "app_name", nullable = false)
    private String appName;
    @Column(name = "api_url", nullable = true)
    private String apiUrl;
    @Column(name = "db_name", nullable = true)
    private String dbName;
    @Column(name = "app_version", nullable = false)
    private String appVersion;
}

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

@Repository
public interface AppConfigRepository extends CrudRepository<AppConfig, Long> {

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

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

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

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

The table class

@Entity
@Table(name = &quot;app_config&quot;)
@EntityListeners(AuditingEntityListener.class)
public class AppConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = &quot;serial&quot;)
private long id;
@Column(name = &quot;app_code&quot;, nullable = false)
private String appCode;
@Column(name = &quot;app_name&quot;, nullable = false)
private String appName;
@Column(name = &quot;api_url&quot;, nullable = true)
private String apiUrl;
@Column(name = &quot;db_name&quot;, nullable = true)
private String dbName;
@Column(name = &quot;app_version&quot;, nullable = false)
private String appVersion;
}

The repository where I am having custom query

@Repository
public interface AppConfigRepository extends CrudRepository&lt;AppConfig, Long&gt; {

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

On running the application I get the exception

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,应该像这样:

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

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

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

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

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:

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

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

@Query(&quot;SELECT n FROM AppConfig n WHERE n.appCode = :appCode and n.appVersion = :appVersion&quot;)
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:

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

答案2

得分: 0

尝试这个:

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

或者你也可以直接使用这个方法

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

try this :

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

or you can use directly the method:

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:

确定