In this post, we will see how to fix the ‘No Property Found for Type’ exception in Spring Data JPA. We may get this exception while server startup. While the server strat up Spring Data JPA loads all repository interfaces and reads custom repository methods. Generally, if we do a mistake in writing custom repository methods, we may end up with a ‘No Property Found for Type’ exception. We are going to discuss the below exception.
Spring Data JPA Interview Questions and Answers
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property name1 found for type Student
Although, there are many possible reasons for this exception. Let’s see a few possible reasons and fixes for this exception.
Consider we have an entity called Student.java
@Entity
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "roll_number")
private String rollNumber;
@Column(name = "university")
private String university;
}
Also, We have our custom repository called StudentRepository.java
@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {
}
- We might get ‘No Property Found for Type’ exception if we do not use field/property name while writing custom repository methods. For below repository methods will throw ‘No Property Found for Type’ exception.
@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {
List<Student> findByName1(String name);
}
In the above example, the repository method should findByName()
instead of findByName1()
.
- While writing the custom repository method we need to make sure we are using proper syntax. For example, findByName() will work fine but findbyName() will throw org.springframework.data.mapping.PropertyReferenceException: No property findbyName found for type Student!
@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {
List<Student> findbyName(String name);
}
Note – Repository methods are case-sensitive. If we use findbyName() instead of findByName() then we will get exception. Check a separate tutorial that tells how to write custom repository methods in Spring Data JPA.
- If we are using more than one field to write the repository method then make sure use ‘And’ properly.
Correct syntax – public List<Student> findByNameAndRollNumber(String name, String rollNumber);
Wrong syntax – public List<Student> findByNameandRollNumber(String name, String rollNumber);
The second one will throw ‘Caused by: org.springframework.data.mapping.PropertyReferenceException: No property nameandRollNumber found for type Student’ exception.
StackTrace for No Property Found for Type’ exception
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findbyName found for type Student!
at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:94) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:382) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:311) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324) ~[na:1.8.0_251]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:293) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:276) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.repository.query.parser.Part.(Part.java:82) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:251) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_251]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_251]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_251]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_251]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_251]
at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:252) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:381) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_251]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_251]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_251]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_251]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_251]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_251]
at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:382) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:94) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
That’s all about how to fix Spring Data JPA No Property Found for Type exception.
Check docs for more details.
You may like other Spring Data JPA tutorials.
- Spring Data JPA greater than Example
- Spring Data JPA less than Example
- Spring Data JPA IsNull Example Using Spring Boot
- Spring Data findById() Vs getOne()
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.