Reputation: 143
I can not figure out this error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'restapiApplication': Unsatisfied dependency expressed through method 'productRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.murphy.demo.model.Product com.murphy.demo.repository.ProductRepository.findOne(java.lang.String)! No property findOne found for type Product!
Here is my Repository in package:
package com.murphy.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.murphy.demo.model.Product;
@Repository
public interface ProductRepository extends JpaRepository<Product,String> {
Product findOne(String id);
}
package com.murphy.demo.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.murphy.demo.model.Product;
import com.murphy.demo.repository.ProductRepository;
@RestController
@RequestMapping(path ="/api/products/" )
public class ProductsController {
private ProductRepository productRepository;
@Autowired
public void productRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@RequestMapping(path ="{id}", method = RequestMethod.GET)
public Product getProduct(@PathVariable(name = "id") String id) {
return productRepository.findOne(id);
}
}
Product.java in package
package com.murphy.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator ="system-uuid")
@GenericGenerator(name ="system-uuid",strategy ="uuid")
private String id;
private String name;
private String description;
private String category;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
main application:
package com.murphy.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.murphy.demo.model.Product;
import com.murphy.demo.repository.ProductRepository;
@SpringBootApplication
public class RestapiApplication implements CommandLineRunner{
private ProductRepository productRepository;
@Autowired
public void productRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public static void main(String[] args) {
SpringApplication.run(RestapiApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Product test = new Product();
test.setName("test");
test.setDescription("test");
test.setCategory("general");
test.setType("null");
productRepository.save(test);
}
}
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.murphy.demo</groupId>
<artifactId>restapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restapi</name>
<description> project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.properites is as follows:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:~/Products;IFEXISTS=FALSE;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/h2-console
spring.jpa.show-sql= true
Ive tried everything I can think of, Any suggestions at all will be helpful.
Thanks!
Upvotes: 1
Views: 750
Reputation: 175
By naming your method "findOne" the Spring Data JPA is trying to create a query to search by for the property "findOne" in your Product Class, which doesn't exist, hence the error "No property findOne found for type Product". You should specify the name of the property you are searching for. In your case: findOneById (String id) will create a query to find one object by the Id property.
For more information on how to name your methods to create the correct queries you can read the documentation about this topic.
Upvotes: 1
Reputation: 165
Why don't you use CrudRepository instead of JpaRepository? findOne(id) will be automatically available. That way your repository interface whould just be empty.
https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
Upvotes: 0