9

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.

これは、リポジトリのオートワイヤリングが行われたときに発生するようです (以下のコード例)。

サービスクラス

@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>

これは、春と 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
4

4 に答える 4

0

com.acme.domain.entitiesエンティティを含むパッケージ ( ) を に追加しますpackagesToScan

于 2016-05-31T09:28:43.193 に答える
0

PersistenceUnitNameの代わりに、を設定します。persistenceXmlLocation

必要ありませんpersistence.xml

factory.setPersistenceUnitName("dummy1");
factory.setPersistenceUnitName("dummy2");
于 2016-06-14T20:34:13.740 に答える
0

私と同じ問題が発生しているかどうかはわかりません.2つの異なるエンティティファクトリを作成しようとしています...例外は同じなので、これが役立つかもしれません. 基本的に、この例外は、間違った EntityManagerFactory でエンティティをインスタンス化しているときに発生していました。

私のプロジェクトは単一のデータベース接続で正常に動作していましたが、2 つ目のデータベース接続を追加すると、エンティティをインスタンス化する場所について EclipseLink が混乱しました。いくつかのデバッグの後、私の結論は、root-context.xml からプロパティ「packagesToScan」を削除し、それを「persistenceXmlLocation」に置き換えることでした。その XML では、スキャンする必要があるすべてのクラスをリストし、それ以外のすべてを「<exclude-unlisted-classes>」で除外しました。

現在動作している完全な XML 構成は次のとおりです。

root-context.xml

<bean id="dataSource1" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://server1.example.com:5432/db1" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <property name="defaultAutoCommit" value="false" />
    <property name="initialSize" value="5" />
    <property name="maxIdle" value="5" />
    <property name="validationQuery" value="SELECT 1" />
    <property name="timeBetweenEvictionRunsMillis" value="600000" />
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="10" />
</bean>

<bean id="dataSource2" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://server2.example.com:5432/db2" />
    <property name="username" value="user" />
    <property name="password" value="password" />
    <property name="defaultAutoCommit" value="false" />
    <property name="initialSize" value="5" />
    <property name="maxIdle" value="5" />
    <property name="validationQuery" value="SELECT 1" />
    <property name="timeBetweenEvictionRunsMillis" value="600000" />
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="10" />
</bean>


<bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource1" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence1.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="false" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.weaving" value="false" />
            <entry key="eclipselink.logging.level" value="WARNING" />
            <entry key="eclipselink.logging.timestamp" value="false" />
            <entry key="eclipselink.logging.session" value="false" />
            <entry key="eclipselink.logging.thread" value="false" />
        </map>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

<bean id="emf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence2.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="false" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.weaving" value="false" />
            <entry key="eclipselink.logging.level" value="WARNING" />
            <entry key="eclipselink.logging.timestamp" value="false" />
            <entry key="eclipselink.logging.session" value="false" />
            <entry key="eclipselink.logging.thread" value="false" />
        </map>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

<bean name="transaction1" class="org.springframework.orm.jpa.JpaTransactionManager" p:entity-manager-factory-ref="emf1" />
<bean name="transaction2" class="org.springframework.orm.jpa.JpaTransactionManager" p:entity-manager-factory-ref="emf2" />

<jpa:repositories base-package="org.myproject.repository1" transaction-manager-ref="transaction1" entity-manager-factory-ref="emf1" />
<jpa:repositories base-package="org.myproject.repository2" transaction-manager-ref="transaction2" entity-manager-factory-ref="emf2" />

<tx:annotation-driven />

persistence1.xml

<persistence-unit name="persistence1" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.myproject.repository1.Repo1</class>
    <class>org.myproject.repository1.Repo2</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

persistence2.xml

<persistence-unit name="persistence2" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.myproject.repository2.Repo1</class>
    <class>org.myproject.repository2.Repo2</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

だから、これは私のために働く...

どうやら、各「persistence.xml」の場所を指定すると、別の EntityManagerFactory 用のクラスが最初の「emf」でインスタンス化されるのを防ぎます。実際、これはhttps://bugs.eclipse.org/bugs/show_bug.cgi?id=338837のバグのようです。デバッグから理解できるのは、「persistenceXmlLocation」がないと、「packageScan」で見つかったすべてのクラスが最初に作成された「emf」でインスタンス化され、2 番目の「emf」が完全に無視されたことです。

お役に立てれば。幸運を!

于 2014-08-02T13:58:08.517 に答える