0

I'm writing updates as part of CRUD testing and when I test my code, I get an error saying no entities are found. I have no idea why, because my partner did the exact same code and he worked perfectly. Neither of us is able to figure out what's going on. I'm getting an error on the getSingleResult() method.

    @Test
public void updateBookTest() {
    Book book = em.createQuery("select b from Book b where b.title = :title", Book.class).setParameter("title", "createABook").getSingleResult();

    tx.begin();
    book.setTitle("updatedThisBook");
    book.setAuthor("newAuthor");
    tx.commit();

    Book updatedBook = em.find(Book.class, book.getBookId());
    assertEquals(book.getTitle(), updatedBook.getTitle());
    assertEquals(book.getAuthor(), updatedBook.getAuthor());
    System.out.println("updateBookTest:\t" + book.toString());

    tx.begin();
    book.setTitle("createABook");
    tx.commit();        
}

This is my code. Let me know if more information is needed.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • maybe no records of Book entity with the provided title in DB? Is your partner querying the same database? As far as I can see, getSingleResult will fail if no records matching the criteria are found in DB. See https://stackoverflow.com/questions/2002993/jpa-getsingleresult-or-null – Repoker Oct 01 '19 at 09:16
  • `getSingleResult` would also fail if more than 1 record was found. Check your table for entities with same title – Nikolai Shevchenko Oct 01 '19 at 09:48
  • Are you both getting information from same database? – Vrian7 Oct 01 '19 at 13:16

1 Answers1

0

getSingleResult must to throw a NoResultException if there is no result.

So, your test is Ok.

Check that both are using same database (and there is no data returned), both are running same query, and both are using same jpa implementation versions.

From javadoc (since jpa 1.0):

getSingleResult

java.lang.Object getSingleResult(): Execute a SELECT query that returns a single untyped result.

Returns: the result

Throws:

  • NoResultException - if there is no result

  • NonUniqueResultException - if more than one result

  • IllegalStateException - if called for a Java Persistence query language UPDATE or DELETE statement

  • QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back

  • TransactionRequiredException - if a lock mode has been set and there is no transaction

  • PessimisticLockException - if pessimistic locking fails and the transaction is rolled back

  • LockTimeoutException - if pessimistic locking fails and only the statement is rolled back

  • PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back

Reference javadoc getSingleResult

Another point, check that your friend is not calling getResultList instead of getSingleResult. This method returns a list and no throw an exception if empty.

Reference javadoc getResultList

Ariel Carrera
  • 5,113
  • 25
  • 36