-2

고객이 특정 월/주 또는 모든 날짜 범위에 대한 모든 주문을 볼 수있는 RESTful 웹 서비스를 만들고 있습니다. Spring-Boot, Spring Data, JPA, Hibernate 및 메모리 데이터베이스 h2를 사용 중입니다. Maven을 사용하여 모든 종속성을 얻었습니다.JPA 저장소를 사용하여 스프링 데이터 액세스

고객 및 주문이라는 두 개의 클래스가 있습니다. 고객은 다수 주문을 할 수 있는데, 이는 그들 사이에 일대 다 (One to Many) 관계가 있음을 의미합니다. 나는 데이터베이스와 localhost : // h2/console에 고객 및 주문 테이블을 채 웁니다. 테이블과 데이터를 볼 수 있습니다. OneToMany 관계로 고객을 주문에 매핑했습니다.

먼저 고객의 모든 주문을 그의 이름으로 얻으려고합니다.

여기 내 고객 기업의 :

@Entity 

공용 클래스 고객 {

@Id 
private Integer customer_id; 
private String customer_first_name; 

@OneToMany(fetch=FetchType.LAZY) 
private List<Customer_Orders> customerOrders; 


protected Customer() { 

} 


public Customer(Integer customer_id, String customer_first_name) { 
    super(); 
    this.customer_id = customer_id; 
    this.customer_first_name = customer_first_name; 
} 

public Integer getCustomer_id() { 
    return customer_id; 
} 

public void setCustomer_id(Integer customer_id) { 
    this.customer_id = customer_id; 
} 

public String getCustomer_first_name() { 
    return customer_first_name; 
} 

public void setCustomer_first_name(String customer_first_name) { 
    this.customer_first_name = customer_first_name; 
} 

@Override 
public String toString() { 
    return "Customer [customer_id=" + customer_id + ", customer_first_name=" + customer_first_name + "]"; 
} 

여기 내 CustomerRepository 클래스입니다입니다 :

:

@Repository 
public interface CustomerRepository extends JpaRepository<Customer, Integer>{ 

    List<Customer_Orders> findAllByFirstName(String customer_first_name); 

} 

가 여기 내 CustomerService를 클래스의 내가 응용 프로그램을 실행할 때

@RestController 
public class CustomerResource { 

    @Autowired 
    private CustomerRepository customerRepository; 

    private CustomerService customerService; 


    @GetMapping("/customers/orders/{customer_first_name}") 
    public List<Customer_Orders> findAllByFirstName(@PathVariable(value="customer_first_name") String customer_first_name) { 
     return customerService.findAllFirstName(customer_first_name); 
    } 

} 

, 내가 무엇입니까 스택 추적 다음 : 0

여기 내 CustomerController 클래스입니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerResource': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.shiptTest.jpa.ShiptTakeHomeTest.customer.CustomerRepository.findAllByFirstName(java.lang.String)! No property firstName found for type Customer! 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:138) ~[spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:751) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:387) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at com.shiptTest.jpa.ShiptTakeHomeTest.ShiptTakeHomeTestApplication.main(ShiptTakeHomeTestApplication.java:10) [classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] 
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.shiptTest.jpa.ShiptTakeHomeTest.customer.CustomerRepository.findAllByFirstName(java.lang.String)! No property firstName found for type Customer! 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    ... 24 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.shiptTest.jpa.ShiptTakeHomeTest.customer.CustomerRepository.findAllByFirstName(java.lang.String)! No property firstName found for type Customer! 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:82) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:208) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:555) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:548) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_91] 
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_91] 
    at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_91] 
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_91] 
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_91] 
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_91] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:550) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:540) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_91] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:540) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:319) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:141) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.util.Lazy.get(Lazy.java:63) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:102) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] 
    ... 34 common frames omitted 
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property firstName found for type Customer! 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:350) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:330) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:283) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324) ~[na:1.8.0_91] 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:265) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:248) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_91] 
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_91] 
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_91] 
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_91] 
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_91] 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:380) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_91] 
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_91] 
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_91] 
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_91] 
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_91] 
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_91] 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:381) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:96) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:76) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE] 
    ... 60 common frames omitted 
+0

당신이 스택 트레이스를 포맷 할 수 있습니다 :

@Service public class CustomerService { @Autowired private CustomerRepository customerRepository; public List<Customer_Orders> findAllFirstName(String customer_first_name) { return customerRepository.findAllByFirstName(customer_first_name); } } 

마침내 당신은 서비스를 스캔해야합니까? – Sid

+2

** ** 오류 메시지를 읽으셨습니까? * "Customer! *"유형의 firstName 속성을 찾을 수 없습니까? 또한,'Repository <'에 위치한'findAllByFirstName'이라는 메소드가'List '를 리턴해야한다고 생각하는 이유는 무엇입니까? 너는 아이스크림을 사려고 차고에 가지 않는다. 그러므로 Repository 으로 이동하여 Customer_Orders를 찾으십시오. 그리고 Java 명명 규칙을 존중하십시오 : CustomerOrder, firstName 등 –

답변

0

메소드 이름 findByFirstName (...)을 사용해보십시오. Spring 데이터는 모든 일치하는 엔티티를 반환합니다. 요점은 엔티티에서 필드가 있어야한다는 것입니다.

private string firstName; Fild입니다의 이름이 fieldName에있는 경우 즉

는, 당신은 방법 findByFieldName()

0

문제는 스프링 데이터 JPA이 CustomerfirstName라는 이름의 속성이 기대한다는 것입니다 선언합니다. 이는 CustomerRepository.findAllByFirstName 메소드가 있기 때문입니다. 그러나 당신의 실체에서 그 필드의 이름은 customer_first_name입니다.

엔터티에서 필드의 이름을 Java best pratice 스타일 가이드 (일반 필드의 밑줄을 사용하는 대신 낙타의 경우) 인 customerFirstName과 일치하도록 이름을 바꾸는 것이 좋습니다.@Query("select c from customer c where c.customer_first_name = ?1")

+0

질문에 Customer Entity를 추가했습니다. –

0

에만

private String firstName; 

대신

사용해야합니다 : 다른 방법은 @Query 주석 CustomerRepository.findAllByFirstName에 사용 된 것 CustomerRepository.findAllByCustomerFirstName

: 이름 바꾸기는 CustomerRepository의 방법은 그래서는 필드 이름과 일치입니다

private String customer_first_name; 

및 @Autowired on 서비스 :

@RestController 
public class CustomerResource { 


    // private CustomerRepository customerRepository; 

    @Autowired 
    private CustomerService customerService; 


    @GetMapping("/customers/orders/{customer_first_name}") 
    public List<Customer_Orders> findAllByFirstName(String customer_first_name) { 
     return customerService.findAllFirstName(customer_first_name); 
    } 

} 

및 서비스 클래스에 생성자를 삭제할 수 있습니다

<context:component-scan base-package="*****" />