找到另一个META-INF / persistence.xml

时间:2016-08-08 10:12:54

标签: java spring jpa

我有一个问题: 我的项目在另一个目录中搜索persistence.xml

代码

    @Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.psc.repository", "com.psc.export.itconfig.repositories"},
        repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class)
@EnableJpaAuditing
public class JpaTestConfig {

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        return vendorAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceUnitName("psc_test");
        return factory;
    }

    @Bean
    public EntityManager entityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

    @Bean
    AuditorAware auditorAware() {
        return (AuditorAware<String>) () -> {
            final String userName = Optional.ofNullable(VaadinService.getCurrentRequest())
                    .map(request -> Optional.ofNullable(request.getUserPrincipal())
                            .map(principal -> principal.getName())
                            .orElse(request.getRemoteUser()))
                    .orElse(AppData.getAppData().getCurrentUser());

            return userName;
        };
    }
}

上面的类路径是test \ java \ configurations \ JpaTestConfig.java

路径test \ resources \ META-INF \ persistence.xml

中有我的xml文件

当我运行测试时,它会在我项目的绝对不同的包中引用另一个persistence.xml。怎么解决?在另一个persistence.xml中甚至不存在unit(psc_test)

测试类

    @RunWith(SpringJUnit4ClassRunner.class) //@SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @ContextConfiguration(classes = {JpaTestConfig.class, ServicesConfig.class, restService.com.websystique.springmvc.configuration.RestConfig.class}) public class BranchControllerTest {

        @Autowired
        private WebApplicationContext wac;



        private MockMvc mockMvc;

        @Before
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();    }

        @Test
        public void getFoo() throws Exception {    /*  code here */
        } 
}

1 个答案:

答案 0 :(得分:0)

首先,有时会出现歧义...为了测试目的,创建一个新的工作区并保留您想要测试的项目。如果它工作那么它将保持不必要的项目,或者它的路径问题。 或者其他..您可以更改持久性名称并检查它是否仍指向另一个persistence.xml。