我的persistenceConfig与spring数据jpa有什么问题

时间:2015-11-08 20:29:40

标签: spring jpa

我使用的是弹簧数据jpa,在我的项目中,我根本没有任何xmls。 我使用纯java配置。使用PersistenceConfig如下。但是我得到一个错误,我不明白如何驾驭它。我是一个业余爱好程序员。

很高兴能得到任何帮助。感谢

    @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "com.jpa" })
    @PropertySources({ @PropertySource("classpath:/database/database.properties") })
    @EnableJpaRepositories(basePackages = {"com.jpa.repository"})
    public class PersistenceConfig
    {
    @Autowired
    Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(User.class.getPackage().getName());
    em.setPackagesToScan(new String[] { "som.jpa.domain" });
    em.setPersistenceUnitName("jpa");

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
    }

    Properties additionalProperties()
    {
    Properties properties = new Properties();

    properties.setProperty("hibernate.hbm2ddl.auto", "auto");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    properties.setProperty("hibernate.show_sql", "true");
    properties.setProperty("entitymanager.packagesToScan", "rapid");


    return properties;
    }

    @Bean(name = "dataSource")
    public DataSource dataSource()
    {
    DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();


     driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
     driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
     driverManagerDataSource.setUsername("root");
     driverManagerDataSource.setPassword("");

    return driverManagerDataSource;
    }

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

    return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
    {
    return new PersistenceExceptionTranslationPostProcessor();
    }


    @Bean
    @Autowired
    @Qualifier(value = "jpaTransactionManager")
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory)
    {
    JpaTransactionManager txManager = new JpaTransactionManager();
    JpaDialect jpaDialect = new HibernateJpaDialect();
    txManager.setEntityManagerFactory(entityManagerFactory);
    txManager.setJpaDialect(jpaDialect);
    return txManager;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
    return new HibernateExceptionTranslator();
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
    return new PersistenceExceptionTranslationPostProcessor();
    }
    }

这是错误:

 org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'entityManagerFactory' defined in   com.jpa.config.PersistenceConfig: Invocation of init method failed; nested   exception is java.lang.NoSuchMethodError:    javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence  /ValidationMode;
    at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initi    alizeBean(AbstractAutowireCapableBeanFactory.java:1574)
  at    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCre     ateBean(AbstractAutowireCapableBeanFactory.java:539)
  ............

0 个答案:

没有答案