【问题标题】:org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruitorg.springframework.data.mapping.PropertyReferenceException:没有找到类型 Fruit 的属性 detachItem
【发布时间】:2016-10-28 11:28:48
【问题描述】:

我正在尝试为 JPA 存储库 (FruitRepository) 添加自定义实现 (FruitRepositoryCustomImpl)。这是我的代码 -

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.persistence.*;

interface FruitRepository extends JpaRepository<Fruit, Long>, FruitRepositoryCustom {

}

interface FruitRepositoryCustom {
    void detachItem(Fruit fruit);
}

@Repository
class FruitRepositoryCustomImpl implements FruitRepositoryCustom {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void detachItem(Fruit fruit) {
        entityManager.detach(fruit);
    }

}

@SpringBootApplication
public class EatFruitApplication {

    public static void main(String[] args) {
        SpringApplication.run(EatFruitApplication.class, args);
    }
}

和实体

@Entity
class Fruit {
    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

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

还有

@RestController
@RequestMapping("/fruits")
class FruitRestController {
    @Autowired
    private FruitRepository fruitRepository;

    @RequestMapping(value = "/{fruitId}", method = RequestMethod.GET)
    public Fruit readItem(@PathVariable Long fruitId) {
        Fruit fruit = this.fruitRepository.findOne(fruitId);
        return fruit;
    }

    @RequestMapping(method = RequestMethod.POST)
    public Fruit addItem(@RequestBody Fruit fruit) {
        fruitRepository.save(fruit);
        return fruit;
    }
}

但我在运行应用程序时遇到以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRestController': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eat.fruit.FruitRepository eat.fruit.FruitRestController.fruitRepository; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed; 
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit!   at 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]     at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]    at 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]     at 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]     at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]  at 
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]    at 
org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]    at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]    at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]   at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]   at 
eat.fruit.EatFruitApplication.main(EatFruitApplication.java:36) [classes/:na]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]    at 
java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]     at 
com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na] Caused by: 
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eat.fruit.FruitRepository 
eat.fruit.FruitRestController.fruitRepository; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit!   at 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]    at 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  ... 22 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed; nested exception is 
org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit!   at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]    at 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]     at 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]     at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]  at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]    at 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   ... 24 common frames omitted 
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit!    at 
org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.11.4.RELEASE.jar:na]    at 
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.11.4.RELEASE.jar:na]    at 
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.11.4.RELEASE.jar:na]  at 
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.11.4.RELEASE.jar:na]  at 
org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.11.4.RELEASE.jar:na]   at 
org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) ~[spring-data-commons-1.11.4.RELEASE.jar:na]  at 
org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:62) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]     at 
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]    at 
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]  at 
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]   at 
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) ~[spring-data-commons-1.11.4.RELEASE.jar:na]     at 
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) ~[spring-data-commons-1.11.4.RELEASE.jar:na]    at 
org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]   at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]    at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]   ... 34 common frames omitted

请帮忙。

谢谢。

【问题讨论】:

  • 该异常的堆栈跟踪会告诉您它的来源...... SpringDataJPA API 最有可能(相对于 JPA API)
  • @NeilStockton 它源自 spring 代码,它试图为 Fruit 类找到 detachItem 属性。我无法找到它为什么要这样做。
  • @NeilStockton 添加了堆栈跟踪。
  • spring boot 是否有可能尝试根据方法名称在后台自动配置自定义接口?尝试改变 void detachItem(Fruit fruit); to void detachFruit(Fruit fruit);
  • @MartinMcKeaveney 将detachItem 更改为detachFruit 后出现同样的错误

标签: spring spring-boot spring-data spring-data-jpa


【解决方案1】:

Spring 对 @PersistenceContext 注释的处理几乎完全符合您的要求,但有一个很大的不同:您总是得到一个事务范围的 EntityManager 并且 Spring 总是为同一个线程注入同一个实例,因此您有一种传播和不必担心线程安全。但是您永远不会以这种方式获得扩展的上下文! 相信我,Spring 3 和扩展的持久性上下文不能很好地结合在一起,也许这会在 Spring 3.1 中改变,但恐怕这不是他们关注的重点。如果您想使用扩展的持久性上下文,让 Spring 注入 EntityManagerFactory(通过 @PersistenceUnit 注释),然后自己创建 EntityManager。对于传播,您必须将实例作为参数传递或自己将其存储在 ThreadLocal 中。

尝试阅读此链接:http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

你可以通过的例子

 public class SomeClient {

      @Autowired
      private PersonRepository repository;

      public void doSomething() {
        List<Person> persons = repository.findByLastname("Matthews");
      }
    }

【讨论】:

  • 我正在使用 Spring 4。很抱歉,由于我是 Spring 新手,所以我无法很好地理解您的回答。您能否提供任何链接,我可以从中了解如何以正确的方式进行操作?
  • 好的。您可以再做一件事,而不是使用@PersistenceContext,您可以使用@Autowired 自动装配存储库,并且可以使用 JpaRepository 中可用的功能与数据库进行交易。 Jpa 提供对 db 进行操作的所有权限。
  • 谢谢。我已经从您提供的链接中关注1.3 Custom implementations for Spring Data repositories。您提供的代码就像存储库的包装器。如果我们可以使用存储库的自定义实现添加自定义功能,为什么我们应该做一些不同的事情?
  • 您还可以在界面中使用 jpa 存储库添加自定义实现。我希望我能在某个地方为您提供帮助。
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 2020-07-14
  • 2019-04-08
  • 2017-05-17
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
相关资源
最近更新 更多