Reputation: 71
i'm recently doing a java tutorial and i'm having a problem with query. At first im adding person to database.
now when i check with query if the person is there, the get firstResult() returns zero :/.
Person class sample, with query:
@Entity
@NamedQuery(name="findQuery", query="select p from Person p where p.id=:id")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
//@Column(name="personId")
private long id;
//@Column(name="personName")
private String name;
//@Column(name="personAge")
private int age;
//@Column(name="personNumber")
private int number;
now class that checks query:
@Override
public String read(long id) {
TypedQuery<Person> createNamedQuery = em.createNamedQuery("findQuery", Person.class);
createNamedQuery.setParameter("id", id);
int firstResult = createNamedQuery.getFirstResult();
System.out.println("test: "+firstResult);
if (createNamedQuery.getFirstResult()>0) {
Person singleResult = createNamedQuery.getSingleResult();
creationMessage = "User: " + singleResult.getName() + " added to database with success";
} else {
creationMessage = "User was not added to database";
}
return creationMessage;
}
getFirstResult returns zero when I try to look person with id 1 :/ . Could someone please help me, what i'm doing wrong ?
I dont know if its important to tell but datasource is JTA. :)
Upvotes: 4
Views: 9315
Reputation: 520978
From the Javadoc for Query#getFirstResult()
:
The position of the first result the query object was set to retrieve. Returns 0 if setFirstResult was not applied to the query object
You never called setFirstResult
, so rightfully getFirstResult
should be returning zero. I suspect that you are confounding this method as being something which can detect if there is a matching record or not. Rather, use the following code for that:
TypedQuery<Person> createNamedQuery = em.createNamedQuery("findQuery", Person.class);
createNamedQuery.setParameter("id", id);
try {
Person singleResult = createNamedQuery.getSingleResult();
creationMessage = "User: " + singleResult.getName()+" added to database with success";
}
catch (Exception e) {
System.out.println("Either less than one row, more than one row, or some other error");
}
getSingleResult()
will throw an exception if no user record be found, or if more than one record be found. By catching the exception you can determine whether the query were successful. Iterating result sets usually work this way; you read one record at a time, and figure out things as you go along.
Upvotes: 3
Reputation: 26502
I think you should try to refactor your solution a bit as the firstResult
is used for pagination purposes mostly and thats not what you are looking for here I presume.
More natural way would be to get the single result and catch and exception if nothing is found:
TypedQuery<Person> createNamedQuery = em.createNamedQuery("findQuery", Person.class);
createNamedQuery.setParameter("id", id);
try{
Person singleResult = createNamedQuery.getSingleResult();
creationMessage = "User: " + singleResult.getName() + " added to database with success";
} catch(NoResultException e) {
creationMessage = "User was not added to database";
}
Upvotes: 1