本文介紹了在不使用持久性文件的情況下以編程方式創建實體管理器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試以編程方式創建實體工廠管理器,而不使用持久性文件
EntityManagerFactory emf;
Map<String, String> properties = new HashMap<String, String>();
properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url", "jdbc:mysql://173.194.25***************");
properties.put("hibernate.connection.username", "etech****");
properties.put("hibernate.connection.password", "A*****");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show-sql", "true");
properties.put("provider", "org.hibernate.ejb.HibernatePersistence");
emf = Persistence.createEntityManagerFactory(idClient, properties);
在線
emf = Persistence.createEntityManagerFactory(idClient, properties);
我收到錯誤:
org.springframework.beans.factory.BeanCreationException:錯誤創建名為‘com*RepositoryFieldsFieldWorkerRepositoryImpl’:的Bean初始化方法調用失敗;嵌套異常為javax.sistence.PersistenceException:沒有名為idClient的實體管理器的持久性提供程序
如何解決此問題?
如有任何幫助,我們將不勝感激
推薦答案
這里是一種純編程方式,可以在沒有Spring和persistence.xml
的情況下構建實體管理器。常量取自org.hibernate.cfg.AvailableSettings
:
entityManagerFactory = new HibernatePersistenceProvider().createContainerEntityManagerFactory(
archiverPersistenceUnitInfo(),
ImmutableMap.<String, Object>builder()
.put(JPA_JDBC_DRIVER, JDBC_DRIVER)
.put(JPA_JDBC_URL, JDBC_URL)
.put(DIALECT, Oracle12cDialect.class)
.put(HBM2DDL_AUTO, CREATE)
.put(SHOW_SQL, false)
.put(QUERY_STARTUP_CHECKING, false)
.put(GENERATE_STATISTICS, false)
.put(USE_REFLECTION_OPTIMIZER, false)
.put(USE_SECOND_LEVEL_CACHE, false)
.put(USE_QUERY_CACHE, false)
.put(USE_STRUCTURED_CACHE, false)
.put(STATEMENT_BATCH_SIZE, 20)
.build());
entityManager = entityManagerFactory.createEntityManager();
和聲名狼藉的PersistenceUnitInfo
private static PersistenceUnitInfo archiverPersistenceUnitInfo() {
return new PersistenceUnitInfo() {
@Override
public String getPersistenceUnitName() {
return "ApplicationPersistenceUnit";
}
@Override
public String getPersistenceProviderClassName() {
return "org.hibernate.jpa.HibernatePersistenceProvider";
}
@Override
public PersistenceUnitTransactionType getTransactionType() {
return PersistenceUnitTransactionType.RESOURCE_LOCAL;
}
@Override
public DataSource getJtaDataSource() {
return null;
}
@Override
public DataSource getNonJtaDataSource() {
return null;
}
@Override
public List<String> getMappingFileNames() {
return Collections.emptyList();
}
@Override
public List<URL> getJarFileUrls() {
try {
return Collections.list(this.getClass()
.getClassLoader()
.getResources(""));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public URL getPersistenceUnitRootUrl() {
return null;
}
@Override
public List<String> getManagedClassNames() {
return Collections.emptyList();
}
@Override
public boolean excludeUnlistedClasses() {
return false;
}
@Override
public SharedCacheMode getSharedCacheMode() {
return null;
}
@Override
public ValidationMode getValidationMode() {
return null;
}
@Override
public Properties getProperties() {
return new Properties();
}
@Override
public String getPersistenceXMLSchemaVersion() {
return null;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public void addTransformer(ClassTransformer transformer) {
}
@Override
public ClassLoader getNewTempClassLoader() {
return null;
}
};
}
請注意,Spring提供了配置持久性的簡化方法,同時支持多個Hibernate版本。(Spring 4.2最高支持Hibernate 5.1,Spring 4.3最高支持5.2)。
這篇關于在不使用持久性文件的情況下以編程方式創建實體管理器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,