From 737430655c632e4baadf6787d0e084995fb9a55b Mon Sep 17 00:00:00 2001
From: Azhwani <13301425+azhwani@users.noreply.github.com>
Date: Sun, 16 Apr 2023 18:32:18 +0200
Subject: [PATCH] BAEL-6110: Fix Spring Data JPA Exception: No Property Found
for Type (#13753)
---
.../spring-boot-data-3/pom.xml | 4 ++
.../NoPropertyFoundApplication.java | 13 ++++++
.../nopropertyfound/config/DbConfig.java | 20 +++++++++
.../nopropertyfound/model/Person.java | 41 +++++++++++++++++++
.../repository/PersonRepository.java | 15 +++++++
.../src/main/resources/application.properties | 7 +++-
.../src/main/resources/data.sql | 3 ++
.../src/main/resources/schema.sql | 6 +++
.../PersonRepositoryIntegrationTest.java | 28 +++++++++++++
9 files changed, 136 insertions(+), 1 deletion(-)
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/NoPropertyFoundApplication.java
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/config/DbConfig.java
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/model/Person.java
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/repository/PersonRepository.java
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/resources/data.sql
create mode 100644 spring-boot-modules/spring-boot-data-3/src/main/resources/schema.sql
create mode 100644 spring-boot-modules/spring-boot-data-3/src/test/java/com/baeldung/nopropertyfound/PersonRepositoryIntegrationTest.java
diff --git a/spring-boot-modules/spring-boot-data-3/pom.xml b/spring-boot-modules/spring-boot-data-3/pom.xml
index fcf7451c61..4903d2ea26 100644
--- a/spring-boot-modules/spring-boot-data-3/pom.xml
+++ b/spring-boot-modules/spring-boot-data-3/pom.xml
@@ -34,6 +34,10 @@
mysql-connector-java
runtime
+
+ com.h2database
+ h2
+
org.springframework.boot
spring-boot-starter-test
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/NoPropertyFoundApplication.java b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/NoPropertyFoundApplication.java
new file mode 100644
index 0000000000..276df9535b
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/NoPropertyFoundApplication.java
@@ -0,0 +1,13 @@
+package com.baeldung.nopropertyfound;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class NoPropertyFoundApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(NoPropertyFoundApplication.class, args);
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/config/DbConfig.java b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/config/DbConfig.java
new file mode 100644
index 0000000000..9a589e55a3
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/config/DbConfig.java
@@ -0,0 +1,20 @@
+package com.baeldung.nopropertyfound.config;
+
+import javax.sql.DataSource;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class DbConfig {
+
+ @Bean
+ @ConfigurationProperties(prefix = "h2.datasource")
+ public DataSource dataSource() {
+ return DataSourceBuilder.create()
+ .build();
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/model/Person.java b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/model/Person.java
new file mode 100644
index 0000000000..3392d3ec67
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/model/Person.java
@@ -0,0 +1,41 @@
+package com.baeldung.nopropertyfound.model;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+public class Person {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private int id;
+ private String firstName;
+ private String lastName;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/repository/PersonRepository.java b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/repository/PersonRepository.java
new file mode 100644
index 0000000000..900c391c93
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/java/com/baeldung/nopropertyfound/repository/PersonRepository.java
@@ -0,0 +1,15 @@
+package com.baeldung.nopropertyfound.repository;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import com.baeldung.nopropertyfound.model.Person;
+
+@Repository
+public interface PersonRepository extends JpaRepository {
+
+ // findByFirsttName will cause Spring Data to throw PropertyReferenceException
+ // Person findByFirsttName(String firstName);
+ Person findByFirstName(String firstName);
+
+}
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/resources/application.properties b/spring-boot-modules/spring-boot-data-3/src/main/resources/application.properties
index cbe044134f..71f39e0ee3 100644
--- a/spring-boot-modules/spring-boot-data-3/src/main/resources/application.properties
+++ b/spring-boot-modules/spring-boot-data-3/src/main/resources/application.properties
@@ -4,4 +4,9 @@ spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=none
-spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
\ No newline at end of file
+spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
+
+h2.datasource.url=jdbc:h2:mem:testdb
+h2.datasource.driver-class-name=org.h2.Driver
+h2.datasource.username=sa
+h2.datasource.password=
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/resources/data.sql b/spring-boot-modules/spring-boot-data-3/src/main/resources/data.sql
new file mode 100644
index 0000000000..5623bbfadf
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/resources/data.sql
@@ -0,0 +1,3 @@
+INSERT INTO person (first_name, last_name) VALUES('Azhrioun', 'Abderrahim');
+INSERT INTO person (first_name, last_name) VALUES('Brian', 'Wheeler');
+INSERT INTO person (first_name, last_name) VALUES('Dave', 'Anderson');
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-data-3/src/main/resources/schema.sql b/spring-boot-modules/spring-boot-data-3/src/main/resources/schema.sql
new file mode 100644
index 0000000000..738ef25298
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/main/resources/schema.sql
@@ -0,0 +1,6 @@
+DROP TABLE IF EXISTS person;
+CREATE TABLE person(
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ first_name VARCHAR(200),
+ last_name VARCHAR(200)
+)
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-data-3/src/test/java/com/baeldung/nopropertyfound/PersonRepositoryIntegrationTest.java b/spring-boot-modules/spring-boot-data-3/src/test/java/com/baeldung/nopropertyfound/PersonRepositoryIntegrationTest.java
new file mode 100644
index 0000000000..2a9d1a46f2
--- /dev/null
+++ b/spring-boot-modules/spring-boot-data-3/src/test/java/com/baeldung/nopropertyfound/PersonRepositoryIntegrationTest.java
@@ -0,0 +1,28 @@
+package com.baeldung.nopropertyfound;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+
+import com.baeldung.nopropertyfound.model.Person;
+import com.baeldung.nopropertyfound.repository.PersonRepository;
+
+@DataJpaTest
+class PersonRepositoryIntegrationTest {
+
+ @Autowired
+ private PersonRepository personRepository;
+
+ @Test
+ void givenQueryMethod_whenUsingValidProperty_thenCorrect() {
+
+ Person person = personRepository.findByFirstName("Azhrioun");
+
+ assertNotNull(person);
+ assertEquals("Abderrahim", person.getLastName());
+ }
+
+}