BAEL-6110: Fix Spring Data JPA Exception: No Property Found for Type (#13753)
This commit is contained in:
parent
8b674cd074
commit
737430655c
|
@ -34,6 +34,10 @@
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<Person, Integer> {
|
||||||
|
|
||||||
|
// findByFirsttName will cause Spring Data to throw PropertyReferenceException
|
||||||
|
// Person findByFirsttName(String firstName);
|
||||||
|
Person findByFirstName(String firstName);
|
||||||
|
|
||||||
|
}
|
|
@ -5,3 +5,8 @@ spring.datasource.password=root
|
||||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||||
spring.jpa.hibernate.ddl-auto=none
|
spring.jpa.hibernate.ddl-auto=none
|
||||||
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
|
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=
|
||||||
|
|
|
@ -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');
|
|
@ -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)
|
||||||
|
)
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue