我尝试使spring数据jpa的java配置无法正常工作。对于Main类,它要求persistence.xml但我根本不使用任何xml。在某种程度上,它没有读我的PersistenceConfig类。
为任何帮助感到高兴
这是我的代码:
@EnableWebMvc
@Configuration
@ComponentScan({ "com.test.*" })
@EnableTransactionManagement
@PropertySources({ @PropertySource("classpath:/database/database.properties") })
@EnableSpringDataWebSupport
@EnableJpaRepositories(basePackages = {"com.test.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[] { "com.test.domain" });
em.setPersistenceUnitName("rapid");
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", "test");
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(this.entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
{
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
@Autowired
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();
}
}
Main类是:
@Transactional
public class Main
{
public static void main(String[] args)
{
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager manager = factory.createEntityManager();
Main test = new Main(manager);
EntityTransaction tx = manager.getTransaction();
tx.begin();
try
{
test.listUsers();
}
catch (Exception e)
{
e.printStackTrace();
}
tx.commit();
test.listUsers();
System.out.println(".. done");
}
}
这是错误:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named rapid: No META-INF/persistence.xml was found in classpath.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.test.main.Main.main(Main.java:35)
第35行是:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
不知道原因,并乐意获得一些建议或解决方案。