我在Spring启动应用程序中有以下配置:
@Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Value("${ds.fake.driverClass}") String driverClass,
@Value("${ds.fake.url}") String url,
@Value("${ds.fake.user}") String user,
@Value("${ds.fake.password}") String password,
@Value("${ds.fake.hibernateDialect}") String hibernateDialect){
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
// bean.setPersistenceUnitName("fake");
bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
bean.setPersistenceUnitManager(persistenceUnitManager(driverClass,url,user,password));
bean.setJpaVendorAdapter(adapter());
bean.setJpaProperties((Properties)fakeJpaProperties(hibernateDialect).get());
bean.setDataSource(fakeDataSource(driverClass,url,user,password));
//bean.setPackagesToScan("my.required.package");
bean.afterPropertiesSet();
return bean;
}
@Bean
public MergingPersistenceUnitManager persistenceUnitManager(
@Value("${ds.fake.driverClass}") String driverClass,
@Value("${ds.fake.url}") String url,
@Value("${ds.fake.user}") String user,
@Value("${ds.fake.password}") String password){
MergingPersistenceUnitManager merger = new MergingPersistenceUnitManager();
// merger.setPersistenceXmlLocations("classpath*:META-INF/fake/persistence.xml");
merger.setPackagesToScan("my.required.package");
// merger.setDefaultDataSource(fakeDataSource(driverClass,url,user,password));
return merger;
}
@Bean
public HibernateJpaVendorAdapter adapter(){
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setGenerateDdl(false);
return adapter;
}
@Bean(initMethod="init",destroyMethod="close")
public static PoolingDataSource fakeDataSource(
@Value("${ds.fake.driverClass}") String driverClass,
@Value("${ds.fake.url}") String url,
@Value("${ds.fake.user}") String user,
@Value("${ds.fake.password}") String password) {
PoolingDataSource bean = new PoolingDataSource();
bean.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
bean.setUniqueName("fakeDataSource");
bean.setMinPoolSize(2);
bean.setMaxPoolSize(10);
bean.setMaxIdleTime(900);
bean.setUseTmJoin(true);
bean.setDeferConnectionRelease(true);
bean.setAutomaticEnlistingEnabled(true);
bean.setAllowLocalTransactions(true);
bean.setIsolationLevel("READ_COMMITTED");
Properties props = new Properties();
props.setProperty("driverClassName", driverClass);
props.setProperty("url", url);
props.setProperty("user", user);
props.setProperty("password", password);
bean.setDriverProperties(props);
return bean;
}
如果我启动我的应用程序,我收到以下错误:
Caused by: java.lang.IllegalStateException: Persistence unit with name 'default' already obtained
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainPersistenceUnitInfo(DefaultPersistenceUnitManager.java:678)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:355)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:307)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
... 21 more
但是,如果我尝试取消注释行bean.setPersistenceUnitName("fake");
,我会收到新错误:
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'fake' found
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainPersistenceUnitInfo(DefaultPersistenceUnitManager.java:674)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:355)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:307)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at com.ixaris.providers.fake.app.boot.FakeAppBooter.entityManagerFactory(FakeAppBooter.java:226)
at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df.CGLIB$entityManagerFactory$2(<generated>)
at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df$$FastClassBySpringCGLIB$$cefb3e29.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df.entityManagerFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 24 more
我怎样才能解决这个问题?或者自己“创建”持久性单元?