2017-09-15 47 views
2

我有兩個實體產品和圖像時,圖像有產品外鍵。春臺關係問題

當我嘗試運行代碼我得到以下錯誤

org.springframework.beans.factory.UnsatisfiedDependencyException: 錯誤創建名爲「mainController」豆:不滿意依賴 通過現場表示「imageRepository 「;嵌套的例外是 org.springframework.beans.factory.BeanCreationException:錯誤 創建名稱爲豆「imageRepository」:失敗init方法 的調用;嵌套的例外是 org.springframework.data.mapping.PropertyReferenceException:沒有 物業找到的產品類型的形象!在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework .beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans.factory.annotation。 AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java :1264) 〜[spring-beans-4.3.11.RELEASE.jar :4.3.11.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans .factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry .getSingleton(DefaultSingletonBeanRegistry.java:230) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans。 factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (DefaultListableBeanFactory.java:761) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) 〜 [spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.context.support.Abstra ctApplicationContext.refresh(AbstractApplicationContext.java:543) 〜[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java :122) 〜[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5。 7.RELEASE.jar:1.5.7.RELEASE]在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [彈簧引導1.5.7.RELEASE.jar:1。org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] org.springframework.boot。 SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [ spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at model.Application.main(Application.java:10)[classes /:na]引起: org.springframework.beans.factory。 BeanCreationException:錯誤 創建名爲'imageRepository'的bean:調用init方法 失敗;嵌套的例外是 org.springframework.data.mapping.PropertyReferenceException:沒有 物業找到的產品類型的形象!在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 〜[彈簧豆-4.3.11.RELEASE.jar:4.3.11.RELEASE]在 org.springframework.beans .factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。 createBean(AbstractAutowireCapableBeanFactory.java:483) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java: 306) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans。 factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:202) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResol veDependency(DefaultListableBeanFactory.java:1138) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066 ) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 〜[spring -beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] ... 19常見的 幀被省略引起者: org.springframework.data.mapping.PropertyReferenceException:否 找到類型爲Image的屬性產品! org.springframework.data.mapping.PropertyPath。(PropertyPath.java:77) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] org.springframework.data.mapping.PropertyPath。創建(PropertyPath.java:329) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 〜[ spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 〜[spring-data-commons-1.13.7.RELEASE .jar:na] at org.springframework。org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework。數據。repository.query.parser.Part。(Part.java:76) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree $ (PartTree.java:247) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree $ Predicate.buildTree(PartTree。 java:398) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree $ Predicate。(PartTree.java:378) 〜 [spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree。(PartTree.java:89) 〜[spring-data-commons-1.13 .7.RELEASE.jar:na]在 org.springframework.data.jpa.repository.query.Par tTreeJpaQuery。(PartTreeJpaQuery.java:64) 〜[spring-data-jpa-1.11.7.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy。的java:103) 〜[彈簧 - 數據 - JP-A-1.11.7.RELEASE.jar:NA]在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) 〜[spring-data-jpa-1.11.7.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) 〜[spring-data -jpa-1.11.7.RELEASE.jar:na]在 org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethod攔截器(RepositoryFactorySupport.java:436) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java: 221) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) 〜[spring- data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) 〜[spring-data-commons-1.13.7 .RELEASE.jar:na]在 org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBe anjava:101) 〜[spring-data-jpa-1.11.7.RELEASE.jar:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 〜[ spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 〜[spring-beans-4.3.11 .RELEASE.jar:4.3.11.RELEASE] ... 29個共同 幀省略

產品實體

@Entity 
@Table(name = "product") 
public class Product { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 

    private String name; 

    private String description; 

    private Integer parent_product_id; 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer 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 Integer getParent_product_id() { 
     return parent_product_id; 
    } 

    @OneToMany 
    @JoinColumn(name = "product_id") 
    public void setParent_product_id(Integer parent_product_id) { 
     this.parent_product_id = parent_product_id; 
    } 


} 

圖片實體

@Entity 
public class Image { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 

    private String type; 

    @ManyToOne 
    private Product product_id; 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public Product getParent_product_id() { 
     return product_id; 
    } 

    public void setProduct_id(Product product_id) { 
     this.product_id = product_id; 
    } 


} 

我ImageRepository

package model; 

import org.springframework.data.repository.CrudRepository; 

import java.util.List; 

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository 
// CRUD refers Create, Read, Update, Delete 

public interface ImageRepository extends CrudRepository<Image, Long> { 

    List<Image> findByProduct_id(String id); 
} 

感謝您的幫助傢伙!

回答

1

你的倉庫應該是:

public interface ImageRepository extends CrudRepository<Image, Long> { 

    List<Image> findByProductId(Integer id); 
} 

Product實體應該是:

@Entity 
@Table(name = "product") 
public class Product { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 

    private String name; 

    private String description; 

    @Transient 
    private Integer parentId; 

    @ManyToOne 
    @JoinColumn(name="parent_id") 
    private Product parentProduct; 

    //getters/setters 

} 

和你Image實體應該是:

@Entity 
@Table(name = "image") 
public class Image { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 

    private String type; 

    @ManyToOne 
    @JoinColumn(name = "product_id") 
    private Product product; 

    //getters/setters 


} 
+0

當我真的叫端點創建一個新的產品,我如何通過產品上的父嗎? – vmfesta

+0

通過調用'setParentProduct'方法 – pleft

+0

好,但它現在接受一個產品對象,我怎麼測試它郵差例如,我可以不通過產品對象 – vmfesta

0

請更改的名稱PRODUCT_ID產品,並從倉庫更改String類型產品! 也可以嘗試此查詢:

@Query("select * from Image image where image.product=:product") 
List<Image> findByProduct(@Param("product") Product product);