2017-09-15 8 views
2

私はProductとImageという2つのエンティティを持っていますが、イメージにはProduct外部キーがあります。スプリングテーブルの関係の問題

私は次のエラーを取得するコードを実行しようとすると

org.springframework.beans.factory.UnsatisfiedDependencyException: エラー「メインコントローラ」の名前を持つBeanを作成:依存関係 は「フィールドを通して表現不満imageRepository ';入れ子になった例外は ですorg.springframework.beans.factory.BeanCreationException:エラー 名前 'imageRepository'のBeanを作成しています:initメソッドの呼び出し が失敗しました。入れ子にされた例外は org.springframework.data.mapping.PropertyReferenceExceptionです:いいえ プロパティの商品がタイプImageのために見つかりました! org.springframework時: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) 〜[4.3.11.RELEASE春豆-4.3.11.RELEASE.jar]で.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at 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) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] org.springframework.beans時: org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 〜[4.3.11.RELEASE春豆-4.3.11.RELEASE.jar]で.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] 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) 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at 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] 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) [spring-boot-1.5.7.RELEASE.jar:1。5.7.RELEASE] org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot。 (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です:いいえ プロパティの商品がタイプImageのために見つかりました! org.springframework.beans時: org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 〜[4.3.11.RELEASE春豆-4.3.11.RELEASE.jar]で.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。 (AbstractAutowireCapableBeanFactory.java:483) にある[spring-beans-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] 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] org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:202) で 〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 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] org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ) 〜[春豆-4.3.11.RELEASE.jar:4.3.11.RELEASE] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)で 〜[春-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] ... 19 common フレームがありません原因: org.springframework.data.mapping.PropertyReferenceException:いいえ プロパティImage typeの製品が見つかりました! 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] org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 〜 spring-data-commons-1.13.7.RELEASE.jar:na] org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 〜[spring-data-commons-1.13.7.RELEASE .jar:na] 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 $ OrPart。(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 $述語(PartTree.java:378) 〜 [spring-data-commons-1.13.7.RELEASE.jar:na] org.springframework.data.repository.query.parser.PartTree。(PartTree.java:89) 〜[spring-data-commons-1.13] .7.RELEASE.jar:na] at 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) 〜[春-データ-JPA-1.11.7.RELEASE.jar:ナ] 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] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethod (RepositoryFactorySupport.java:438) 〜[spring-data-commons-1.13.7.RELEASE.jar:na] 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] org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) 〜[spring-data-commons-1.13.7 .RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBe an.java:101) 〜[スプリングデータJPA-1.11.7.RELEASE.jar:NA] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 〜[におけるspring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] にあるorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 〜[spring-beans-4.3.11] .RELEASE.jar:4.3.11.RELEASE] ... 29の共通 フレームは省略

Productエンティティ

@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

いいですが、今はProductオブジェクトを受け取っています。私は製品オブジェクトを渡すことができないので、どうやってPostmanでテストしますか? – vmfesta

0

product_idからの製品の名前を変更し、文字列タイプをリポジトリから製品に変更してください。 はまた、このクエリを試してみてください。

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