春季表关系问题

我有两个实体Product和Image,图像有一个Product外键。

当我尝试运行代码时,出现以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'mainController'的bean时出错:通过字段'imageRepository'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'imageRepository'的bean时出错:init方法的调用失败; 嵌套异常是org.springframework.data.mapping.PropertyReferenceException:找不到类型为Image的属性产品! 在org.springframework.beans的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]。 factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor (AbstractAutowireCapableBeanFactory.java:1264)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)〜[spring-beans-4.3.11.RELEASE.jar :4.3.11.RELEASE] at org.springframework.beans.factory.support.AbstractA 在org.springframework.beans.factory.support.AbstractBeanFactory中添加utowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] $ 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]在org.springframework上的org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]。 beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.context.support.Abst ractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)〜[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.context.support.AbstractApplicationContext.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。在org.springframework上的org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]上的RELEASE.jar:1.5.7.RELEASE] .boot.SpringApplication.refreshContext(SpringApplication.java:360)[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at 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]在org.spri ngframework.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:找不到类型为Image的属性产品! 在org.springframework.beans.factory的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]。 (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]在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:30 2)〜[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]在org.springframework上的org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)〜[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]。 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.da ta.mapping.PropertyReferenceException:未找到类型为Image的属性产品! (org.springframework.data.mapping.PropertyPath。(PropertyPath.java:77)〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(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.data.mapping.PropertyPath.from(PropertyPath.java:243)〜[spring-data-commons-1.13.7.RELEASE.jar:na] at org.springframework.data.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 $ 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] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery。( 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)〜在[spring-data-jpa-1.11.7.RELEASE.jar: 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.reposit ory.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。(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] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)〜[spring-data-jpa-1.11.7.RELEASE.jar:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:168 7)〜[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);
}

谢谢你们的帮助!


您的资料库应该是:

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


}

请将product_id的名称更改为product ,并将字符串类型从存储库更改为Product! 另请试试这个查询:

@Query("select * from Image image where image.product=:product")
List<Image> findByProduct(@Param("product") Product product);
链接地址: http://www.djcxy.com/p/60301.html

上一篇: Spring table relationship problems

下一篇: Spring Boot and Google SQL