1. Overview

In this short article, we’ll shed light on how to fix the Spring Data JPA exception PropertyReferenceException: No property found for type.

First, we’ll explain the leading causes behind this exception. Then, we’ll illustrate how to reproduce it using practical examples, and finally, how to fix it.

2. The Cause

Before diving deep into the details, let’s try to understand what the exception means.

The stack trace “No property found for type” simply tells us that a specified property is not found. Spring Data throws this exception when it fails to access a property that doesn’t exist or hasn’t been defined.

Typically, Spring Data automatically generates SQL queries from the names of the derived query methods. So, one of the most common causes behind the exception is defining a query method with invalid properties.

Another reason would be misspelling a property name when accessing the property.

3. Reproducing the Exception

Now that we know what the exception means, let’s see how to reproduce it in practice.

For instance, let’s consider the Person entity class:

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String firstName;
    private String lastName;

    // standard getters and setters
}

Now, let’s create a Spring Data JPA repository for our Person class:

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {
}

Next, we’ll add a query method to get a person by first name. However, let’s pretend to misspell the firstName property.

For example, let’s write firsttName instead:

Person findByFirsttName(String lastName);

Now, if we start our Spring Boot application, we’ll get:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository' defined in com.baeldung.nopropertyfound.repository.PersonRepository ...
...
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract com.baeldung.nopropertyfound.model.Person com.baeldung.nopropertyfound.repository.PersonRepository.findByFirsttName(java.lang.String)! 
No property 'firsttName' found for type 'Person'; Did you mean 'firstName'
...
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'firsttName' found for type 'Person'; Did you mean 'firstName'
...

As we can see in the logs, Spring Boot fails with PropertyReferenceException: No property ‘firsttName’ found for type ‘Person’.

Spring Data automatically checks if the generated SQL queries are valid at startup.

Since we’re using a property that doesn’t exist (firsttName), Spring Data fails to generate the SQL query, hence the exception.

4. The Solution

There’s only one way to fix the exception: to use the exact property names when defining query methods.

So, to fix the issue, we need to rename findByFirsttName() to findByFirstName().

Person findByFirstName(String firstName);

Now, let’s confirm using a test case that everything works as expected:

@Test
void givenQueryMethod_whenUsingValidProperty_thenCorrect() {

    Person person = personRepository.findByFirstName("Azhrioun");

    assertNotNull(person);
    assertEquals("Abderrahim", person.getLastName());
}

As shown above, the query method findByFirstName() doesn’t fail and does the job of returning a person by first name.

In nutshell, here are some key points to keep in mind to avoid PropertyReferenceException:

  • Use the exact property names when defining query methods.
  • Avoid using abbreviations or misspelled names.
  • Double-check the entity class to make sure that the property we want to reference exists and is spelled correctly.
  • Use a code editor with autocomplete features to reduce the risk of misspelling property names.
  • Write unit or integration tests for query methods to ensure that they’re returning the expected results.

5. Conclusion

To sum it up, we discussed in detail what causes Spring Data to throw PropertyReferenceException: No property found for type.

Along the way, we used practical examples to illustrate how to produce the exception and how to fix it.

As always, the full source code of the examples is available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.