简体   繁体   English

JAVA API 不返回任何结果。 在服务器上运行的 SQL 查询确实

[英]JAVA API returning no results. SQL query run on server does

I have a JAVA RESTful API built using NetBeans.我有一个使用 NetBeans 构建的 JAVA RESTful API。 I've created a custom method to retrieve data using a field that isn;t the Primary Key.我创建了一个自定义方法来使用不是主键的字段来检索数据。 If I run this SQL command on the Database itself, I get data however if I run it via the API I get 0 results.如果我在数据库本身上运行这个 SQL 命令,我会得到数据,但是如果我通过 API 运行它,我会得到 0 结果。

Here is the Named Query :这是命名查询

@NamedQuery(name = "KeywordsView.findByCharId", query = "SELECT k FROM KeywordsView k WHERE k.charId = :charId")

The method calling this query :调用此查询的方法

@GET
@Path("findcharid/{charId}")
@Produces({MediaType.APPLICATION_JSON})
public List<KeywordsView> findByCharId(@PathParam("CharId") Integer charId){
    return getEntityManager().createNamedQuery("KeywordsView.findByCharId", KeywordsView.class).setParameter("charId", charId).getResultList();
}

I've debugged the URL and it is calling and running the URL as expected.我已经调试了 URL,它正在按预期调用和运行 URL。

Anything clearly wrong?有什么明显的错误吗?

The charId value is null due to the case not match for the "CharId" in @PathParam("CharId") with {charId} in @Path("findcharid/{charId}") .所述charId值为空由于不匹配的情况下"CharId"@PathParam("CharId"){charId}@Path("findcharid/{charId}") And this make no result as null comparison is always false.这不会产生结果,因为空比较总是错误的。

@GET
@Path("findcharid/{charId}")
@Produces({MediaType.APPLICATION_JSON})
                                       // Should match the case
public List<KeywordsView> findByCharId(@PathParam("CharId") Integer charId){
    System.out.println("charId:"+charId);
    return getEntityManager().createNamedQuery("KeywordsView.findByCharId", KeywordsView.class).setParameter("charId", charId).getResultList();
}

the selected field k name is similar to the table KeywordsView alias k too, not sure but this could be an issue?所选字段k名称也与表KeywordsView别名k相似,不确定,但这可能是一个问题? maybe the when qry executed it generates an exception and that's why you have 0 result?也许当 qry 执行它会产生一个异常,这就是为什么你有 0 结果?

try to change your query as below:尝试更改您的查询如下:

@NamedQuery(name = "KeywordsView.findByCharId", query = "SELECT k FROM KeywordsView kv WHERE kv.charId = :charId")

I have now fixed this.我现在已经解决了这个问题。 It was in my method, there is a sneaky Capital C at "CharId" where is should be "charId".在我的方法中,在“CharId”处有一个偷偷摸摸的大写 C,应该是“charId”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM