英文:
Data exists in DB but JPA returning blank, Spring?
问题
以下是翻译好的部分:
查询数据库以获取结果,但得到空数组
Repository 类
@Repository
public interface AppConfigRepository extends CrudRepository<AppConfig, Long> {
@Query("SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);
}
Table 类
@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;
}
Mapping 函数
@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) {
System.out.println(appCode);
System.out.println(appVersion);
return configRepository.findByCodeAndVersion(appCode, appCode);
}
生成 URL 的方式
http://localhost:9090/api/v1/config?appCode=MANDL_LIVE&appVersion=12.0
日志中显示的查询语句
select appconfig0_.id as id1_0_, appconfig0_.api_url as api_url2_0_, appconfig0_.app_code as app_code3_0_, appconfig0_.app_name as app_name4_0_, appconfig0_.app_version as app_vers5_0_, appconfig0_.db_name as db_name6_0_ from app_config appconfig0_ where appconfig0_.app_code=? and appconfig0_.app_version=?
英文:
I am querying the DB to get the result but I get blank array
The repository class
@Repository
public interface AppConfigRepository extends CrudRepository<AppConfig, Long> {
@Query("SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);
}
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 mapping function
@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) {
System.out.println(appCode);
System.out.println(appVersion);
return configRepository.findByCodeAndVersion(appCode, appCode);
}
This is how I am making the URL
http://localhost:9090/api/v1/config?appCode=MANDL_LIVE&appVersion=12.0
In the logs I see
select appconfig0_.id as id1_0_, appconfig0_.api_url as api_url2_0_, appconfig0_.app_code as app_code3_0_, appconfig0_.app_name as app_name4_0_, appconfig0_.app_version as app_vers5_0_, appconfig0_.db_name as db_name6_0_ from app_config appconfig0_ where appconfig0_.app_code=? and appconfig0_.app_version=?
答案1
得分: 2
你正在调用:
return configRepository.findByCodeAndVersion(appCode, appCode);
难道不应该是:
return configRepository.findByCodeAndVersion(appCode, appVersion);
英文:
You are calling:
return configRepository.findByCodeAndVersion(appCode, appCode);
Shouldn't it be:
return configRepository.findByCodeAndVersion(appCode, appVersion);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论