The following snippet works in Play for Scala:
class MyDAO @Inject() (jpaApi: JPAApi) {
@Transactional
def someMethod = {
jpaApi.withTransaction { // ....
In application.conf I defined db.default.jndiName=DefaultDS and jpa.default=defaultPersistenceUnit.
Now, I also need to define another JNDI connection db.another.jndiName=AnotherDS with jpa.another=anotherPersistenceUnit.
Where the persistence.xml is:
<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>
How to inject AnotherDS in the application so it can be used with JPAApi?
You can specify multiple JPA configurations in
application.conf:In your DAO, inject
JPAApias you're currently doing. UseJPAApi#em(String)to get theEntityManagerfor a specific persistence unit name:Also, the
@Transactionalannotation is unnecessary if you're usingJPAApi#withTransaction.