英文:
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 = "/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);
}
The table class
@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;
}
The repository where I am having custom query
@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);
}
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("SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);
You can also use named parameter inside the query string like this:
@Query("SELECT n FROM AppConfig n WHERE n.appCode = :appCode and n.appVersion = :appVersion")
List<AppConfig> 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<AppConfig> 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("SELECT FROM AppConfig n WHERE n.appCode = :x and n.appVersion = :y")
List<AppConfig> findByCodeAndVersion(@Param("x")String appCode,@Param("y") String appVersion);
or you can use directly the method:
List<AppConfig> findByAppCodeAndAppVersion(String appCode,String appVersion);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论