未找到密钥类的[ManagedType]

时间:2014-10-27 07:46:58

标签: spring-data-jpa eclipselink

我正在使用JPA,H2,EclipseLink,Java Config(无xml)。我的问题是,我在下面找到 No [ManagedType]的密钥类错误。它建议将<exclude-unlisted-classes>false</exclude-unlisted-classes>添加到我的persistence.xml,但我没有使用xml进行配置,因此我不确定如何修复它。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.foo.data.repository.RepositoryTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.foo.data.repository.UserRepository com.foo.data.repository.RepositoryTest.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.foo.data.model.User] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.foo.data.model.User</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
   at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
   at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
   at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

以下是我@Configuration课程的摘录。

@ComponentScan(
      basePackages = { "com.foo.*"},
      excludeFilters = @Filter(type = FilterType.ANNOTATION,
      value = Configuration.class))
@Configuration
@EnableJpaRepositories(basePackages = "com.foo.data.repository")
public class SeleniumAppTestConfig implements InitializingBean {

   @Bean
   public DataSource dataSource() {
      EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
      return builder.setType(EmbeddedDatabaseType.H2)
            .addScript("SeleniumCreateScript.sql").build();
   }

   @Bean
   public Properties jpaProperties() {
       Properties props = new Properties();
       props.setProperty("eclipselink.ddl-generation", "create-tables");
       props.setProperty("eclipselink.weaving", "false");
       props.setProperty("eclipselink.logging.level", "FINEST");
       props.setProperty("eclipselink.logging.parameters", "true");
       return props;
   }

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      final PersistenceProvider persistenceProvider = new PersistenceProvider();

      final EclipseLinkJpaVendorAdapter vendorAdapter =
            new EclipseLinkJpaVendorAdapter();
      vendorAdapter.setGenerateDdl(true);
      vendorAdapter.setShowSql(true);
      vendorAdapter.setDatabase(Database.H2);

      LocalContainerEntityManagerFactoryBean emf =
            new LocalContainerEntityManagerFactoryBean();
      emf.setDataSource(dataSource());
      emf.setPackagesToScan("com.foo.data.model");
      emf.setLoadTimeWeaver(new SimpleLoadTimeWeaver());
      emf.setPersistenceProvider(persistenceProvider);
      emf.setPersistenceUnitName("SeleniumTests");
      emf.setJpaVendorAdapter(vendorAdapter);
      emf.setJpaProperties(jpaProperties());
      emf.afterPropertiesSet();
      return emf;
   }

   @Bean
   public PlatformTransactionManager transactionManager(){
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(
            entityManagerFactory().getObject() );

      return transactionManager;
   }

   @Bean
   public EclipseLinkJpaDialect exceptionTranslator() {
      return new EclipseLinkJpaDialect();
   }

}

这是我的实体类(在com.foo.data.model package中):

@Entity
@Table(name = "USER", schema = "SELENIUM")
public class User {
   @Id
   @Column (name = "USERNAME")
   private String username;
   @Column (name = "PASSWORD")
   private String password;

   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }
   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }

   @Override
   public String toString() {
      return String.format(
            "User[username=%s, password='%s']",
            username, password);
   }

}

这是我的存储库:

@Repository
public interface UserRepository extends CrudRepository <User, String> {
   List<User> findByLastUsername(String username);
}

最后,这是我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumAppTestConfig.class)
public class RepositoryTest {

   /** Logger for this class. */
   private static final Logger LOG =
         Logger.getLogger(RepositoryTest.class);

   @Autowired
   private UserRepository userRepository;

   @Test
   public void testThings() {
      final Iterable<User> users = userRepository.findAll();
      for (User user: users) {
         LOG.info("user [" + user + "].");
      }
   }
}

5 个答案:

答案 0 :(得分:3)

使用@EntityScan上的SeleniumAppTestConfig指定实体基础包。

答案 1 :(得分:2)

这里的问题是通过persistence.xml进行的配置和组件扫描的混合。该设置定义了一个持久性单元名称(在LocalContainerEntityManagerFactoryBean的设置中),该名称使Spring可以使用它,并丢弃无法扫描的软件包,因为它不能扩展现有的持久性单元。

在这种情况下,通过将调试日志记录激活为DefaultPersistenceUnitManager.readPersistenceUnitInfos(…) logs a message可以揭示这一点。

我已提交a ticket来改进错误配置状态下的日志记录,以使问题更加明显。

答案 2 :(得分:1)

只需为每个PersistenceUnitName指定一个虚拟名称。 不需要persistence.xml

factory.setPersistenceUnitName( “dummy1”); factory.setPersistenceUnitName( “dummy2”);

答案 3 :(得分:0)

更新至Eclipse链接2.7.8-RC1

答案 4 :(得分:-6)

通过转移到Hibernate而不是EclipseLink解决了我的问题。