java 在没有持久性文件的情况下以编程方式创建实体管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29434121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create entity manager programmatically without persistence file
提问by tamtoum1987
I'm trying to create entity Factory manager programmatically without persistence file
我试图在没有持久性文件的情况下以编程方式创建实体工厂管理器
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);
On line
在线的
emf = Persistence.createEntityManagerFactory(idClient, properties);
I am getting the error:
我收到错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com****RepositoryFieldsFieldWorkerRepositoryImpl': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named idClient
org.springframework.beans.factory.BeanCreationException:创建名为 'com****RepositoryFieldsFieldWorkerRepositoryImpl' 的 bean 时出错:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: No Persistence provider for EntityManager named idClient
How can i resolve this problem ?
我该如何解决这个问题?
any help will be appreciated
任何帮助将不胜感激
回答by Brice
Here is a pure programmatic way to build an entity manager without spring and without a persistence.xml
. Constants are taken from org.hibernate.cfg.AvailableSettings
:
这是一种纯粹的编程方式来构建没有 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();
And the infamous PersistenceUnitInfo
还有臭名昭著的 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;
}
};
}
Note that Spring offers streamlined way to configure the persistence, while supporting multiple hibernate versions. (Spring 4.2 supports Hibernate up to 5.1, Spring 4.3 supports Hibernate up to 5.2).
请注意,Spring 提供了配置持久性的简化方法,同时支持多个休眠版本。(Spring 4.2 最高支持 Hibernate 5.1,Spring 4.3 最高支持 Hibernate 5.2)。
回答by mssch
A persistence.xml
file is mandatory to create your persistence unit at deployment time as per the JPA specs.
根据persistence.xml
JPA 规范,在部署时创建持久性单元必须有一个文件。
See Create JPA EntityManager without persistence.xml configuration file