在 Play for Scala 中声明用于 JPA 访问的非默认 JNDI 连接
Declaring non-default JNDI connection for JPA access in Play for Scala
以下代码片段适用于 Play for Scala:
class MyDAO @Inject() (jpaApi: JPAApi) {
@Transactional
def someMethod = {
jpaApi.withTransaction { // ....
在application.conf
中我定义了db.default.jndiName=DefaultDS
和jpa.default=defaultPersistenceUnit
。
现在,我还需要定义另一个 JNDI 连接 db.another.jndiName=AnotherDS
和 jpa.another=anotherPersistenceUnit
。
其中 persistence.xml
是:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HANAColumnStoreDialect"/>
</properties>
</persistence-unit>
<persistence-unit name="anotherPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>AnotherDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HANAColumnStoreDialect"/>
</properties>
</persistence-unit>
</persistence>
如何在应用程序中注入 AnotherDS
以便它可以与 JPAApi 一起使用?
您可以在 application.conf
中指定多个 JPA 配置:
db.default.jndiName=DefaultDS
... // other configuration for db.default
jpa.default=defaultPersistenceUnit
db.another.jndiName=AnotherDS
... // other configuration for db.another
jpa.another=anotherPersistenceUnit
在您的 DAO 中,像您目前所做的那样注入 JPAApi
。使用 JPAApi#em(String)
获取特定持久性单元名称的 EntityManager
:
class MyDAO @Inject() (jpaApi: JPAApi) {
def someMethodWithDefault = {
val em = jpaApi.em("default") // em is an EntityManager
jpaApi.withTransaction {
...
}
}
def someMethodWithAnother = {
val em = jpaApi.em("another") // em is an EntityManager
jpaApi.withTransaction {
...
}
}
此外,如果您使用 JPAApi#withTransaction
,则不需要 @Transactional
注释。
以下代码片段适用于 Play for Scala:
class MyDAO @Inject() (jpaApi: JPAApi) {
@Transactional
def someMethod = {
jpaApi.withTransaction { // ....
在application.conf
中我定义了db.default.jndiName=DefaultDS
和jpa.default=defaultPersistenceUnit
。
现在,我还需要定义另一个 JNDI 连接 db.another.jndiName=AnotherDS
和 jpa.another=anotherPersistenceUnit
。
其中 persistence.xml
是:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HANAColumnStoreDialect"/>
</properties>
</persistence-unit>
<persistence-unit name="anotherPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>AnotherDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HANAColumnStoreDialect"/>
</properties>
</persistence-unit>
</persistence>
如何在应用程序中注入 AnotherDS
以便它可以与 JPAApi 一起使用?
您可以在 application.conf
中指定多个 JPA 配置:
db.default.jndiName=DefaultDS
... // other configuration for db.default
jpa.default=defaultPersistenceUnit
db.another.jndiName=AnotherDS
... // other configuration for db.another
jpa.another=anotherPersistenceUnit
在您的 DAO 中,像您目前所做的那样注入 JPAApi
。使用 JPAApi#em(String)
获取特定持久性单元名称的 EntityManager
:
class MyDAO @Inject() (jpaApi: JPAApi) {
def someMethodWithDefault = {
val em = jpaApi.em("default") // em is an EntityManager
jpaApi.withTransaction {
...
}
}
def someMethodWithAnother = {
val em = jpaApi.em("another") // em is an EntityManager
jpaApi.withTransaction {
...
}
}
此外,如果您使用 JPAApi#withTransaction
,则不需要 @Transactional
注释。