Eclipselink与spring不兼容?

时间:2016-02-17 11:24:56

标签: java spring eclipselink

我正在尝试将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有关还是更多?

由于

1 个答案:

答案 0 :(得分:0)

这是以某种方式避开问题,因为我不知道你的问题的原因是什么,但是我正在使用Spring Data JPA和EclipseLink,没有问题。

使用Spring Data JPA,您可以按如下方式重新编写代码:

public interface TestEntityRepository extends JpaRepository<TestEntity, String> {
}

因此像这样做你的查询。看不到任何有名的查询。

List<TestEntity> testData = testEntityRepository.findAll();

有关更多详细信息,请参阅Spring Data JPA文档。