- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 JPA 存储库 ( FruitRepositoryCustomImpl
) 添加自定义实现 ( FruitRepository
)。这是我的代码 -
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
最佳答案
Spring 对 @PersistenceContext 注释的处理几乎完全符合您的要求,但有一个很大的不同:您总是得到一个事务范围的 EntityManager 并且 Spring 总是为同一个线程注入(inject)同一个实例,所以你有一种传播而不是不得不担心线程安全。但是您永远不会以这种方式获得扩展的上下文!
相信我,Spring 3 和扩展的持久性上下文不能很好地结合在一起,也许这会在 Spring 3.1 中改变,但恐怕这不是他们关注的重点。如果您想使用扩展的持久性上下文,让 Spring 注入(inject) 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 - org.springframework.data.mapping.PropertyReferenceException : No property detachItem found for type Fruit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037200/
我有: class fruit { } class apple:fruit { } class pear:fruit { } 我想创建一个函数“addFruit”来将水果添加到水果 vector 中。
题目地址: https://leetcode.com/problems/fruit-into-baskets/description/ 题目描述: Ina row of trees, the i-
这个问题与金融产品、利率和决定某种产品的利息的两个属性有关。但我认为水果和篮子更容易形象化。 首先,我们有水果。水果可以有特定的大小(小、中、大)和颜色(红、绿、蓝)。这将是两个不同的枚举。篮子里装着
这个问题在这里已经有了答案: What is PECS (Producer Extends Consumer Super)? (16 个答案) 关闭 3 年前。 我遇到了一个问题,例如: 水果类 p
我可以在 this tutorial 中找到以下代码,我不明白 public int compareTo(Fruit compareFruit) { //I don't understand
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以使溢出。 3个月前关闭。
我在 Android 设备上运行 Fruit Ninja 应用程序时进行了一些带宽和频率测量。 (这是用例测试过程的一部分。)我创建了一个自动获取 CPU 频率数据的脚本。该脚本每 30 秒更改一次实
当用户在 Unix shell 中输入 FRUIT=apple 时,如何从输入的 FRUIT=apple 中分别获取名称 FRUIT 和 apple? echo -e "\nEnter inputs
对于样式表,如果有一种方法可以在样式表中关联类,那就太好了。例如,如果我有一个名为 fruit 的类和一个名为 apple 的类,如果我能说 apple 也是一种水果,那就太好了。 我知道这可以通过
鉴于我有一个设置了一些属性的 Fruit 实例,并且我想将这些属性放入一个新的 Pear 实例中(因为这个特定的 Fruit 恰好具有梨的品质),实现此目标的最佳方法是什么效果? 例如,我们不能做的只
有关“苹果”的 50 条推文见下文。我已经手动标记了有关 Apple Inc. 的正面匹配项。它们在下面标记为 1。 这里有几行: 1|“@chrisgilmer: Apple targets big
行 val a: Set[Fruit]=Set[Apple] 不编译,为什么? 如何解决这个问题? package sandbox.stackOverFlow class Fruit class Ap
我正在创建一个游戏,用户可以在场景中移动一些水果。我希望用户只能移动场景中的水果,而不能移动场景中的任何其他 SKSpriteNode,因此我编写了下面的代码来实现它。然而,代码无法正常工作,因为我似
我正在尝试为 JPA 存储库 ( FruitRepositoryCustomImpl ) 添加自定义实现 ( FruitRepository )。这是我的代码 - import org.springf
我想知道如何像“水果忍者”中的水果一样使用 OpenGles 旋转 Sprite ,我使用的是 cocos2d,这意味着我想使用 openGLEs 在 3D 中旋转 CCSprite。 谢谢回答。 最
尝试在 prisma 中查询并从相关对象中过滤结果但出现错误: Unknown arg 'where' in select.fruit.where for type UserFruit. Did yo
我是一名优秀的程序员,十分优秀!