无法使用基于代码的配置初始化EntityManagerFactory

时间:2013-10-25 19:35:34

标签: spring-mvc entitymanager

我已经从XML配置转移到基于代码的配置。 这是我的JpaContext.java课程:

@Configuration
@ComponentScan(basePackages = { "com.foo.bar.repository", "com.foo.bar.domain" })
@EnableJpaRepositories(basePackages={"com.foo.bar.repository"})
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, order = 0)  // is like <tx:annotation-driven/>
@PropertySource( {"classpath:persistence.properties" } )

public class MyJpaContext {
    private static final Logger logger = LoggerFactory.getLogger(MyJpaContext.class);

    @Autowired
    Environment env;

    private @Value("${persistence.db.driverClass}") String driverClass;
    private @Value("${persistence.db.url}") String url;
    private @Value("${persistence.db.username}") String userName;
    private @Value("${persistence.db.password}") String passWord;

    @Bean
    public DataSource dataSource() {
        System.out.println(">>>>> Creating DataSource...");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        logger.info(String.format("Using JDBC driver: %1$s", driverClass));
        logger.info(String.format("Using DB URL: %1$s", url));
        logger.info(String.format("Logging into DB with username: <%1$s> and password <%2$s>", userName, passWord));
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(env.getProperty("persistence.db.url"));
        dataSource.setUsername(userName);
        dataSource.setPassword(passWord);
        System.out.println("<<<<< DataSource successfully created.");
        return dataSource;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
        System.out.println(">>>>> Creating HibernateJpaVendorAdapter...");
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql          (env.getProperty("persistence.hibernate.showSql", Boolean.class     ));
        adapter.setGenerateDdl      (env.getProperty("persistence.hibernate.generateDdl", Boolean.class ));
        adapter.setDatabasePlatform (env.getProperty("persistence.hibernate.databasePlatform"           ));
        System.out.println("<<<<< HibernateJpaVendorAdapter successfully created.");
        return adapter;
    }

    @Bean
    //  @DependsOn({"dataSource"})
    public EntityManagerFactory entityManagerFactory() {
        System.out.println("Creating EntityManagerFactory...");
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPersistenceXmlLocation("classpath*:META-INF/persistence.xml");
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.foo.bar.repository"); //this implicitly generates an appropriate persistence.xml for us at runtime . 
        em.setJpaVendorAdapter(jpaVendorAdapter());
        em.setJpaProperties(additionalProperties());
        em.setPersistenceUnitName("My_PU");
        em.setPersistenceProviderClass(org.hibernate.ejb.HibernatePersistence.class);
        em.afterPropertiesSet();
        System.out.println("<<<<< EntityManagerFactory successfully created.");
        return em.getObject();
    }

    Properties additionalProperties() {
        return new Properties() {
            {  // Hibernate Specific:
                setProperty("hibernate.hbm2ddl.auto", "update");
                setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
            }
        };
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        System.out.println("Creating JpaTransactionManager...");
        return new JpaTransactionManager(entityManagerFactory());
    }

//  @Bean
//  public PlatformTransactionManager txManager() {
//      System.out.println(env.getProperty("moka.session.timeout"));
//      return new DataSourceTransactionManager(dataSource());
//  }

    @Bean
        public PersistenceExceptionTranslator hibernateExceptionTranslator() {
        System.out.println(">>>>> Creating HibernateExceptionTranslator...");
        return new HibernateExceptionTranslator();
    }
}

调用em.afterPropertiesSet();时,我遇到了

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: 
Factory method [public javax.persistence.EntityManagerFactory
com.foo.bar.config.MyJpaContext.entityManagerFactory()] threw exception; 
nested exception is 
java.lang.TypeNotPresentException: 
Type com.foo.bar.domain.AbcTicket not present

当我开始TOMCAT时......尽管这个AbcTicket类确实存在。

我错过了什么,Spring能够初始化我的Web应用程序?

在从基于XML的配置迁移到基于代码的配置(逐个元素)之前,我能够启动我的Web应用程序。

1 个答案:

答案 0 :(得分:0)

好像这是一个重构问题。 我删除了所有出现和与AbcClass的关系并再次重新创建它们......现在它运行良好。