I am trying to use JPA through WSO2 ESB. I have my entities in Module A, which is bundled to Module B, because Module A generates JAR and in WSO2 OSGi modules should be used.
In Module A I have got:
- entities package with all entities
-
persistence.xml under /resources/META-INF folder
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <non-jta-data-source>jdbc/PathToJNDI</non-jta-data-source> <class>com.moduleA.entities.Locks</class> <class>com.moduleA.entities.Logs</class> <class>com.moduleA.entities.MessageContent</class> <class>com.moduleA.entities.Messages</class> <class>com.moduleA.entities.Parts</class> <class>com.moduleA.entities.Recipients</class> <class>com.moduleA.entities.Test</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.persistence-context.flush-mode" value="commit" /> </properties>
To get persistence properties I am using JNDI. It is configured in WSO2 panel.
And these elements are packed to Module B via maven-bundle-plugin.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Export-Package>com.coig,com.coig.*,pl.coig.*, com.moduleA.entities.*</Export-Package>
<DynamicImport-Package>*</DynamicImport-Package>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
<JPA-PersistenceUnits>JPAPU</JPA-PersistenceUnits>
<Import-Package>
*, org.hibernate*,org.hibernate.proxy, javassist.util.proxy,
javax.persistence;version="1.1.0";jpa="2.0",
com.microsoft.sqlserver.jdbc, ${extra.packages},
javax.transaction.*,
*;resolution:=optional,
org.osgi.framework
</Import-Package>
<Embed-Dependency>entities</Embed-Dependency>
</instructions>
</configuration>
</plugin>
After Maven building I can see that Module B Manifest contains all things from configuration.
Then, I am trying to get list of all records from database in WSO2 Mediator Project in mediate() method.
Map<String, Object> properties = new HashMap<>();
properties.put("openjpa.ConnectionDriverName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
properties.put("openjpa.ConnectionFactoryName","jdbc/PathToJNDI");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPU",properties);
try{
EntityManager entityManager = emf.createEntityManager();
try{
log.severe("EntityManagerFactory and Entity manager created");
entityManager.getTransaction().begin();
try{
log.severe("Transaction started");
Query q = entityManager.createNamedQuery("Test.findAll", Test.class);
List<Test> list = q.getResultList();
for ( Test s : list ) {
log.severe(s.getName());
}
entityManager.getTransaction().commit();
} catch (Exception e1){
if(entityManager.getTransaction().isActive()){
entityManager.getTransaction().rollback();
}
log.log(Level.INFO, e1.getMessage(), e1);
}
}catch (Throwable e){
log.severe("EntityManager exception: " + e.getMessage());
}finally {
entityManager.close();
}
}catch(Exception e2){
log.severe("EntityManagerFactory exception: " + e2.getMessage());
}finally {
emf.close();
}
Is it necessary to provide driver and factory name in properties to map and then use it to create EntityManagerFactory? Without these properties I get warning, that OpenJPA does not find Driver of Datasource.
I am getting message that app did not find classpath for Test.class, despite it is provided in Maven dependencies and package with entities is imported in maven-bundle-plugin under MediatorProject pom. I do not want to add to MediatorProject Path folder with entities, but I want to import them via maven-bundle or some other way (if you know how to make it works :))
What I should change in my configuration to satisfy the JPA transaction?
Tags: jpa