我可以像这样获取 JPA 配置:
Configuration jpaConf = Configuration.root().getConfig("jpa");
但是我怎样才能获得 org.hibernate.cfg.Configuration ,我需要像这个问题一样进行架构导出(基于 Play Framework 1):Using SchemaExport in Play Framework
我的 Play Framework 2.x application.conf 有以下内容:
# Database configuration
# ~~~~~
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/dpma"
db.default.user=bp
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
我的 persistence.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<!--<property name="hibernate.default_schema" value=""legalEntitiesTest""/>-->
<property name="hibernate.globally_quoted_identifiers" value="false"/>
</properties>
</persistence-unit>
</persistence>
**对于那些不熟悉 Play 框架的人:** https://playframework.com/documentation/2.2.4/JavaJPA
最佳答案
您可以使用 hibernate 配置文件自己创建 hibernate 配置对象:
Configuration c = new Configuration();
c.configure("path to hibernate config").getProperty("hibernate property");
如果您不使用 hibernate cfg 文件,则可以通过这种方式将 JPA 实体添加到配置中(它假设所有 JPA 类都属于一个包):
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(EntityClass.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
在执行架构导出之前,请记住设置方言:
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
关于hibernate - 如何使用 JPA 插件获取 Play Framework 2 中的 org.hibernate.cfg.Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25871914/