在Scala中声明JPA访问的非默认JNDI连接
以下代码段适用于Scala:在Scala中声明JPA访问的非默认JNDI连接,scala,playframework,playframework-2.5,Scala,Playframework,Playframework 2.5,以下代码段适用于Scala: class MyDAO @Inject() (jpaApi: JPAApi) { @Transactional def someMethod = { jpaApi.withTransaction { // .... 在application.conf中,我定义了db.default.jndiName=DefaultDS和jpa.default=defaultPersistenceUnit 现在,我还需要用jpa.other=ano
class MyDAO @Inject() (jpaApi: JPAApi) {
@Transactional
def someMethod = {
jpaApi.withTransaction { // ....
在application.conf
中,我定义了db.default.jndiName=DefaultDS
和jpa.default=defaultPersistenceUnit
现在,我还需要用jpa.other=anotherPersistenceUnit
定义另一个JNDI连接db.other.jndName=AnotherDS
其中,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>
org.hibernate.jpa.HibernatePersistenceProvider
默认值
org.hibernate.jpa.HibernatePersistenceProvider
其他人
如何在应用程序中注入另一个JPA配置,以便与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
。用于获取特定持久性单元名称的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#with transaction
,则不需要@Transactional
注释