Eclipselink和Spring数据

标签 eclipselink spring-data-jpa

我正在尝试结合使用eclipselink(2.4.0)和spring-data-jpa(1.1.0.RELEASE)来实现一个解决方案。但是,每次部署解决方案(Tomcat 7)时,都会出现以下异常:

Caused by: java.lang.IllegalArgumentException: No [ManagedType] was found for 
the key class [com.acme.domain.entities.User] in the Metamodel - please 
verify that the [Managed] class was referenced in persistence.xml using a 
specific <class>com.acme.domain.entities.User</class> property or a global 
<exclude-unlisted-classes>false</exclude-unlisted-classes> element.

当存储库 Autowiring 发生时,它似乎发生了(下面的代码示例):

服务等级
@Component
public class UserDataService {

    @Autowired
    private UserRepository userRepository;

    ...
}

实体类
package com.acme.domain.entities;

...

@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Integer id;

    ...

}

persistence.xml
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    
    <class>com.acme.domain.entities.User</class>

    ...
</persistence-unit>

这可能是由于spring和eclipselink之间的某些冲突造成的吗?

更新:

堆栈跟踪...
[#|2012-07-31 16:25:02,317|ERROR|pool-2-thread-40|org.springframework.web.context.ContextLoader|Context initialization failed|#]
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDataService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.acme.data.repositories.UserRepository com.acme.data.services.UserDataService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: No [ManagedType] was found for the key class [com.acme.domain.entities.User] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.acme.domain.entities.User</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:897)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:873)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.acme.data.repositories.UserRepository com.acme.data.services.UserDataService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: No [ManagedType] was found for the key class [com.acme.domain.entities.User] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.acme.domain.entities.User</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:512)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 27 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: No [ManagedType] was found for the key class [com.acme.domain.entities.User] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.acme.domain.entities.User</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:484)
    ... 29 more
Caused by: java.lang.IllegalArgumentException: No [ManagedType] was found for the key class [com.acme.domain.entities.User] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.acme.domain.entities.User</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
    at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entityEmbeddableManagedTypeNotFound(MetamodelImpl.java:174)
    at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:489)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:58)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:87)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:70)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:137)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 37 more

最佳答案

我能够解决我的问题,但这是上面的另一种设置,我只添加了偶然发现的几件事,直到能够在我的SE环境中解决它:

  • 错误https://bugs.eclipse.org/bugs/show_bug.cgi?id=338837影响EclipseLink 2.2.0及更低版本。大多发生在本地环境(RESOURCE_LOCAL)中。 EclipseLink版本升级或以下解决方法可能会有所帮助:

  • <exclude-unlisted-classes>false</exclude-unlisted-classes>
    
  • 在类路径中有多个persistence.xml文件(例如,特别是在第3方JAR中),它们都定义相同的持久性单元名称(“默认”以上可能很常见)。在这种情况下,请尝试重命名您的持久性单元名称,作为第一步
  • 如果重命名有效的持久性单元名称,但是将其更改回旧值会重现此问题,则可能是上述第三方JAR或您自己项目中的旧JAR包含在您的构建中(例如,对项目进行重构/重命名)级别或类似级别更改了结构,例如,Maven构建仍可以从存储库中获取旧的构建工件)

  • 最后,当涉及到我的设置时(Spring-Standalone Application,没有persistence.xml,而是大量的Spring配置,因为使用了自定义公司Framework),这表明我只是忘了添加自定义Framework-Core的实体包当我想在本地项目中运行其服务实现之一(Spring-Bean实现)而不是从JAR依赖项获得其服务实现时。因此,请确保已为EntityManagerFactory列出了所有必需的实体包:

        <bean id="customEntityManagerFactory" parent="abstractEntityManagerFactory">
            <property name="dataSource" ref="dataSource"/>
    
    
            <!-- Packages of EntityClasses -->
            <property name="packagesToScan">
                <list merge="true">
                    <value>ch.company.div1.foo.bar.model</value>
                    <value>ch.company.div1.foo.barnicle.model</value>
                    <value>ch.company.div2.we.help.model</value>
                </list>
            </property>
    
            <property name="jpaProperties">
                <props merge="true">
                    <prop key="eclipselink.target-database">org.eclipse.persistence.platform.database.SQLServerPlatform
                    </prop>
                    <prop key="javax.persistence.transactionType">RESOURCE_LOCAL</prop>
                </props>
            </property>
         </bean>
    

    在我的情况下,我忘记将模型封装与我们公司的另一个部门(写自定义框架核心)的使用过的实体一起添加。

    祝你好运,那个错误是野兽:/

    关于Eclipselink和Spring数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11723208/

    相关文章:

    java - JBoss Eclipselink 不在 postgresql 中的 create-tables/create-or-extend-tables 上创建索引

    java - 在weblogic控制台上查看eclipse link JPA执行的SQL查询或使用log4j配置的日志文件

    java - 如何将 EclipseLink 对象导出到 XML

    java - Service层应该写什么方法?

    java - 如何将以毫秒为单位的 java.time.LocalTime 映射为以毫秒为单位的 java.sql.Time

    java - 如何在 Hibernate native 查询中使用常量?

    jpa - eclipselink连接池

    maven - Eclipselink 元模型生成在 Maven 多模块项目中失败

    java - 如何在 JPA 中获取外键引用以具有 "ON UPDATE CASCADE ON DELETE CASCADE"功能?

    java - 迁移 spring boot 2.1.3 -> 2.1.4 导致数据 jpa 测试出现问题