[英]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: 除此之外,您还必须:
javax.persistence.jdbc.url
parameter). javax.persistence.jdbc.url
参数)。 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,但也许这是一个良好的开端)。 persistence.xml
file. persistence.xml
文件。 This option is much more complicated, but also more flexible.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.