C.Brown
C.Brown

Reputation: 51

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

I have a JAVA RESTful API built using NetBeans. 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.

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.

Anything clearly wrong?

Upvotes: 2

Views: 246

Answers (3)

samabcde
samabcde

Reputation: 8114

The charId value is null due to the case not match for the "CharId" in @PathParam("CharId") with {charId} in @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();
}

Upvotes: 1

C.Brown
C.Brown

Reputation: 51

I have now fixed this. It was in my method, there is a sneaky Capital C at "CharId" where is should be "charId".

Upvotes: 0

Yazan
Yazan

Reputation: 6082

the selected field k name is similar to the table KeywordsView alias k too, not sure but this could be an issue? maybe the when qry executed it generates an exception and that's why you have 0 result?

try to change your query as below:

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

Upvotes: 0

Related Questions