Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getFirstResult() returns 0

Tags:

java

mysql

jpa

jpql

i'm recently doing a java tutorial and i'm having a problem with query. At first im adding person to database.

image 1: enter image description here

image 2: enter image description here

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. :)

like image 268
ravencrest Avatar asked Oct 17 '25 03:10

ravencrest


2 Answers

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.

like image 102
Tim Biegeleisen Avatar answered Oct 19 '25 15:10

Tim Biegeleisen


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";
} 
like image 41
Maciej Kowalski Avatar answered Oct 19 '25 16:10

Maciej Kowalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!