在许多关系spring jpa中注入自动连接的依赖项失败

时间:2015-09-14 14:17:33

标签: java spring spring-mvc jpa

我有两个实体

@Entity
public class Customer {    
    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    @ManyToMany(mappedBy = "vidoes", cascade = {CascadeType.MERGE}) 
    private List<Video> vidoes;
}

@Entity
public class Video {


    @Id
    @GeneratedValue
    private Integer id;

    private String title;   

    @ManyToMany(cascade = {CascadeType.MERGE})
    @JoinTable(name = "videos_customers",
                joinColumns = @JoinColumn(name = "videos_id"),
                inverseJoinColumns = @JoinColumn(name = "customers_id"))
    private List<Customer> customer;

}
在CustomerRepository中的

我想声明一个findByVideo(视频视频)的方法为

public interface CustomerRepository extends JpaRepository<Customer, Integer> {
    List<Customer> findByVideo(Video video);

}

上述声明给我例外

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.db.repository.CustomerRepository com.project.db.service.CuctomerService.customerRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property video found for type Customer!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5014)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:717)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.db.repository.CustomerRepository com.project.db.service.CustomerService.customerRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property video found for type Customer!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 23 more

主要原因是

  

找不到类型为客户的属性视频!

更新

通过以下我可以将数据保存到客户,视频,视频_客户表

@Transactional
@Service
public class InitializeDatabaseService {

    @Autowired
    private VideoRepository videoRepository;

    @Autowired
    private CustomerRepository customerRepository;

    @PostConstruct
    public void initialize() {

        Customer customer = new Customer();

        customer.setName("Raiese Turur Khan");

        customerRepository.save(customer);

        Customer customer2 = new Customer();
        customer2.setName("Gul Mar Jaan Zardan");

        customerRepository.save(customer2);

        Video video1 = new Video();         
        video1.setTitle("Video Title");
        List<Customer> customers = new ArrayList<Customer>();
        customers.add(customer);
        customers.add(customer2);
        video1.setCustomers(customers);
        videoRepository.save(video1);

        Video video2 = new Video();         
        video2.setTitle("Video Title");
        List<Customer> customers2 = new ArrayList<Customer>();
        customers2.add(customer);
        customers2.add(customer2);
        video2.setCustomers(customers2);
        videoRepository.save(video2);       

    }
}

我已经在网上搜索了很多但是找不到任何好的教程或资源来解决上面的“多对多”映射中的问题。

0 个答案:

没有答案