简体   繁体   English

MFP Java适配器上的JPA

[英]JPA on MFP Java adapter

I've created a Java adapter in MFP 7.0. 我已经在MFP 7.0中创建了Java适配器。 The adapter is running on the local development server (Liberty). 适配器正在本地开发服务器(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? 由于我在文档中找不到任何参考,因此是否有可能在Java适配器中使用JPA访问数据库数据?

Where do I need to put the persistence.xml? 我需要将persistence.xml放在哪里? Is there any configuration I have to do on the Liberty profile server.xml? 我在Liberty概要文件server.xml上需要做任何配置吗? Where do I need to put the DB driver's library jar (EclipseLink)? 我需要在哪里放置数据库驱动程序的库jar(EclipseLink)?

Attached you'll find the code from the Java adapter: 附件中,您将找到Java适配器中的代码:

@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: 这是我的persistence.xml的样子:

<?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>

That is definitely possible. 那绝对是可能的。 The location of the persistence.xml file should be as described here . 该位置persistence.xml文件应说明这里 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). 使用使用JNDI的连接(在这种情况下,您不需要javax.persistence.jdbc.url参数)。
  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). 沿到javax.persistence.jdbc.url参数把用户证书到您的数据库中的persistence.xml(如果你去这个选项谷歌的它,因为它依赖于JPA / EclipseLink的vesion,但也许是一个良好的开端)。
  3. Instantiate manually EVERYTHING, in which case you do not need the persistence.xml file. 手动全部实例化,在这种情况下,您不需要persistence.xml文件。 This option is much more complicated, but also more flexible. 此选项复杂得多,但也更灵活。 I did it with Hibernate once, thus cannot help you with EclipseLink. 我曾经使用过Hibernate,因此无法使用EclipseLink。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MFP 7.0 Java适配器构建错误 - MFP 7.0 Java Adapter Build Error 如何将用户异常从MFP 8 Java适配器引发到客户端? - How to throw User Exceptions from MFP 8 Java Adapter to Client? 使用ANT在MFP 7.1中编译/部署Java适配器时出现问题 - Issue In Compiling/Deploying Java Adapter in MFP 7.1 using ANT 如何仅使用Java创建MFP推送通知适配器(不使用Javascript) - How to create an MFP push notification adapter using Java only (no Javascript) 无法在MFP 7.0 Java适配器中发送电子邮件JavaMail API - Unable to send email JavaMail API in MFP 7.0 Java adapter 无法使用MFP v7中的适配器构建器任务(ANT)编译Java适配器 - Cannot compile Java adapter using adapter-builder task (ANT) in MFP v7 示例IBM MFP Java适配器导致导入错误。 FWLSE2302E压缩内容格式错误 - Sample IBM MFP Java adapter causes an error on import. FWLSE2302E Malformed ziped content IBM Mobilefirst JAVA 适配器在存储用户数据时导致 MFP-Conflict=并发失败 - IBM Mobilefirst JAVA adapter giving MFP-Conflict=Concurrency failure while storing user data Spring Integration Java DSL中的JPA出站通道适配器配置 - JPA outbound channel adapter config in Spring Integration Java DSL 具有事务轮询器Java配置的Spring Integration JPA入站通道适配器 - Spring Integration JPA inbound channel adapter with transactional poller Java configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM