【发布时间】:2016-02-17 11:24:56
【问题描述】:
我正在尝试使 eclipselink 适应我正在使用 spring 的项目。当我尝试使用 eclipselinks EntityManagerFactory 创建命名查询时
public class Test {
private static final String FIND_ALL_TEST_ENTITIES = "SELECT te.id FROM TestEntity te";
public Test(EntityManager entityManager){
EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
entityManagerFactory.addNamedQuery("FIND_ALL_TEST_ENTITIES", entityManager.createQuery(FIND_ALL_TEST_ENTITIES));
}
}
我收到此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test' defined in class path resource [com/github/bilak/eclipselinkspringbug/config/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.github.bilak.eclipselinkspringbug.test.Test]: Factory method 'test' threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy61 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at com.github.bilak.eclipselinkspringbug.Application.main(Application.java:22) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.github.bilak.eclipselinkspringbug.test.Test]: Factory method 'test' threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy61 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 18 common frames omitted
Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy61 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.addNamedQuery(EntityManagerFactoryImpl.java:625) ~[eclipselink-2.6.2.jar:2.6.2.v20151217-774c696]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:388) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:541) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at com.sun.proxy.$Proxy56.addNamedQuery(Unknown Source) ~[na:na]
at com.github.bilak.eclipselinkspringbug.test.Test.<init>(Test.java:18) ~[classes/:na]
at com.github.bilak.eclipselinkspringbug.config.Config.test(Config.java:17) ~[classes/:na]
at com.github.bilak.eclipselinkspringbug.config.Config$$EnhancerBySpringCGLIB$$3e1c89b1.CGLIB$test$0(<generated>) ~[classes/:na]
at com.github.bilak.eclipselinkspringbug.config.Config$$EnhancerBySpringCGLIB$$3e1c89b1$$FastClassBySpringCGLIB$$38d568ea.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at com.github.bilak.eclipselinkspringbug.config.Config$$EnhancerBySpringCGLIB$$3e1c89b1.test(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 19 common frames omitted
我在 github 上创建了sample project。调用 mvn clean spring-boot:run 查看问题。这个错误是和eclipselink有关还是和spring more有关?
谢谢
【问题讨论】:
-
你为什么还要注册这样的命名查询而不是使用默认的
orm.xml。如果您已经想要这个 yu,则需要在ApplicationListener中执行此操作,该@PersistenceContext在EntityManager字段上具有EntityManager以检索事务实例。这里不能保证 EntityManger 已经完全准备好。 -
我正在使用另一个以这种方式工作的框架here,所以我只是在寻找如何将所有内容加在一起的方法。
-
它也使用托管实体管理器,而您只是在注入它。如果您使用的是 Axon,请像他们一样注入提供程序。
-
我不确定我是否理解你。我也尝试做类似@Bean EntityManagerProvider entityManagerProvider() { return new ContainerManagedEntityManagerProvider(); } 在我的项目中,但这对我没有帮助。 here 是我配置的轴突 poc。
-
“你为什么还要注册这样的命名查询”?你的意思是他为什么使用 JPA 标准 方法?该方法适用于有人想要动态注册命名查询的情况,并且是一个有效的用例。也许 Spring 不支持它(?),但这不是不支持它的理由。
标签: java spring eclipselink