java IBM Websphere JPA 配置 - 如何更新persistence.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3526434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IBM Websphere JPA configuration - how to update persistence.xml
提问by jobinbasani
I'm new to EJB 3 and the JPA.
我是 EJB 3 和 JPA 的新手。
I've created a datasource in the appserver which is jdbc/AppDataSource. The default persistence provider is left as com.ibm.websphere.persistence.PersistenceProviderImpl.
And I left the default jta data source JNDI name as AppDataSourceitself.
I'm actually confused regarding JTA and non-JTA. What differentiates them?
我在 appserver 中创建了一个数据源,它是jdbc/AppDataSource. 默认的持久性提供程序保留为com.ibm.websphere.persistence.PersistenceProviderImpl. 我将默认的 jta 数据源 JNDI 名称保留为AppDataSource它本身。我实际上对 JTA 和非 JTA 感到困惑。他们有什么区别?
I generated the entities and created an EntityTransactionobject in the bean.
Upon calling the persist()and commit()methods, I get an error:
我生成了实体并EntityTransaction在 bean 中创建了一个对象。调用persist()和commit()方法后,出现错误:
javax.ejb.EJBException: See nested exception; nested exception is: <openjpa-1.2.1-SNAPSHOT-r422266:686069 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.
Caused by: <openjpa-1.2.1-SNAPSHOT-r422266:686069 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.
How does openjpa comes into picture here?
openjpa 是如何出现在这里的?
As of now, my persistence.xmlcontains the entity class names only.
截至目前,我persistence.xml的仅包含实体类名称。
How can I make it use the default appserver values for data source. Or else how can I provide the details of jpa provider and datasource details in the persistence.xml?
如何使它使用数据源的默认应用程序服务器值。否则我如何在persistence.xml.
Please provide your inputs.
请提供您的意见。
回答by Pascal Thivent
This question actually involves many concepts and I won't cover them all in details. For more exhaustive details, I suggest reading the whole Chapter 5 - Entity Managers and Persistence Contextsof the JPA 1.0 specification (and the other relevant sections mentioned at the end of this answer). I will rather try to describe the most common scenario in a Java EE environment.
这个问题其实涉及很多概念,我就不一一详述了。有关更详尽的详细信息,我建议阅读整个第 5 章 -JPA 1.0 规范的实体管理器和持久化上下文(以及本答案末尾提到的其他相关部分)。我宁愿尝试描述 Java EE 环境中最常见的场景。
Within an EJB environment, one would typically use:
在 EJB 环境中,通常会使用:
- A container-managed entity manager(must be a JTA entity manager)
- transaction-scopedin Stateless Session Beans (SLSB)
- extendedin Stateful Session Beans (SFSB)
- JTA Transaction Management (and not resource-local transactionsunless this is reallywhat you want)
- 一个容器管理的实体管理器(必须是JTA实体管理器)
- 无状态会话 Bean (SLSB) 中的事务范围
- 在有状态会话 Bean (SFSB) 中扩展
- JTA 事务管理(而不是资源本地事务,除非这确实是您想要的)
Here is how a persistence.xmlfor a JTA entity manager that uses a data source with a JNDI name java:comp/env/jdbc/AppDataSourcecan be set up, OpenJPA being the persistence provider used by WebSphere:
以下是如何设置persistence.xml使用具有 JNDI 名称的数据源的 JTA 实体管理器的方法java:comp/env/jdbc/AppDataSource,OpenJPA 是 WebSphere 使用的持久性提供程序:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="MyPu" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>java:comp/env/jdbc/AppDataSource</jta-data-source>
<class>com.acme.domain.Foo</class>
<class>com.acme.domain.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- OpenJPA specific properties -->
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
<property name="openjpa.jdbc.DBDictionary" value="db2"/>
</properties>
</persistence-unit>
</persistence>
For the OpenJPA properties, refer to the OpenJPA documentation.
有关 OpenJPA 属性,请参阅 OpenJPA文档。
And here is how a SLSB (using Container Managed Transactions) could get a container managed Entity Manager injected:
以下是 SLSB(使用容器管理事务)如何注入容器管理的实体管理器:
@Stateless
public class EmployeeServiceBean implements EmployeeService {
@PersistenceContext(unitName="MyPu")
private EntityManager em;
public Employee createEmployee(int id, String name, long salary) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}
...
}
And that's all. The life cycle of the entity manager is managed by the container transparently for the application (no createEM/close) and the entity manager participates in the JTA transaction managed by the container (no explicit begin/commit).
就这样。实体管理器的生命周期由容器为应用程序透明管理(无 createEM/close),实体管理器参与容器管理的 JTA 事务(无显式开始/提交)。
As previously hinted, I'm just scratching the surface, my goal is somehow to put you on the right path. To go further, I suggest grabbing a book (e.g. EJB3 in Action). Meanwhile, the references below would be a good reading.
正如之前所暗示的,我只是触及皮毛,我的目标是以某种方式让你走上正确的道路。为了更进一步,我建议拿一本书(例如 EJB3 in Action)。同时,下面的参考资料将是一个很好的阅读。
References
参考
- JPA 1.0 specification
- Section 5.2.1 "Obtaining an Entity Manager in the Java EE Environment"
- Section 5.5 "Controlling Transactions"
- Section 5.6 "Container-managed Persistence Contexts"
- Section 6.2.1.2 "transaction-type"
- Section 6.2.1.4 "provider"
- Section 6.2.1.5 "jta-data-source, non-jta-data-source"
- JPA 1.0 规范
- 第 5.2.1 节“在 Java EE 环境中获取实体管理器”
- 第 5.5 节“控制交易”
- 第 5.6 节“容器管理的持久化上下文”
- 第 6.2.1.2 节“交易类型”
- 第 6.2.1.4 节“提供者”
- 第 6.2.1.5 节“jta-data-source,非 jta-data-source”

![java 将 ImageOutputStream 转换为 byte[]](/res/img/loading.gif)