2

I've created a Java adapter in MFP 7.0. The adapter is running on the local development server (Liberty). Since I couldn't find any references in the documentation, is there a possibility to use JPA within the Java adapter to access DB data?

Where do I need to put the persistence.xml? Is there any configuration I have to do on the Liberty profile server.xml? Where do I need to put the DB driver's library jar (EclipseLink)?

Attached you'll find the code from the Java adapter:

@GET
public String performJPAQuery(){
    String result = null;

    Person marco = new Person();
    marco.setId(1);
    marco.setName("Marco");

    // connection details should be loaded from persistence.xml
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-test");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();

    tx.begin();

    em.persist(marco);

    tx.commit();

    // Querying the contents of the database using JPQL query
    Query q = em.createQuery("SELECT p FROM Person p");

    @SuppressWarnings("unchecked")
    List<Person> results = q.getResultList();

    logger.info("List of persons\n----------------");

    for (Person p : results) {
        logger.info(p.getName() + " (id=" + p.getId() + ")");
    }

    // Closing connection
    em.close();

    emf.close();

    return result;
}

This is how my persistence.xml looks like:

<?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="jpa-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.sample.Person</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:sample.db" />
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>
    </persistence-unit>
</persistence>
mmetting
  • 91
  • 6
  • While I don't know about JPA, here's the tutorial for using SQL in Java adapters: https://developer.ibm.com/mobilefirstplatform/documentation/getting-started-7-0/server-side-development/java-adapter/java-sql-adapter/ – Idan Adar Mar 27 '15 at 07:56
  • Hi Idan, thanks. I've seen the sample, but I'm looking for JPA support. – mmetting Mar 27 '15 at 08:17

1 Answers1

0

That is definitely possible. The location of the persistence.xml file should be as described here. Besides that, you will have to either:

  1. use the connection using JNDI (in which case you do not need the javax.persistence.jdbc.url parameter).
  2. along to the javax.persistence.jdbc.url parameter put the user credentials to your DB in persistence.xml (if you go for this option google for it, as it depends on the JPA/EclipseLink vesion, but probably this is a good start).
  3. Instantiate manually EVERYTHING, in which case you do not need the persistence.xml file. This option is much more complicated, but also more flexible. I did it with Hibernate once, thus cannot help you with EclipseLink.
Community
  • 1
  • 1
V G
  • 18,822
  • 6
  • 51
  • 89
  • Hi Andrei, thank you for your reply. I'm currently facing the problem, that the persistence.xml cannot be encapsulated within the Java adapter. On deploying the adapter, MFP claims, that no other xml file can be deployed within an adapter: "Adapter deployment failed: Adapters can not contain more than one XML file" – mmetting Mar 27 '15 at 12:12
  • I cannot help you with the problems from the MFP side. If there is really no solution to that, option 3 would be a solution, but you will have to instantiate the `EntityManagerFactory` manually. – V G Mar 27 '15 at 12:52
  • 1
    The "Adapter can not contain more than one XML file" constrain is clearly a defect. Until it will be fixed, you can put your persistence.xml in a JAR and put this JAR under the adapter's lib folder (it is harder to maintain, but possible to bypass the defect) – Yotam Madem Mar 30 '15 at 21:27