数据在数据库中存在,但是JPA返回为空,是Spring的问题吗?

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

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&lt;AppConfig, Long&gt; {

@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);
}

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 mapping function

@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) {
	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&amp;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);

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

发表评论

匿名网友

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

确定