IBM Websphere JPA配置-如何更新persistence.xml [英] IBM Websphere JPA configuration - how to update persistence.xml
问题描述
我是EJB 3和JPA的新手.
I'm new to EJB 3 and the JPA.
我已经在appserver中创建了一个数据源,该数据源是jdbc/AppDataSource
.默认的持久性提供程序保留为com.ibm.websphere.persistence.PersistenceProviderImpl
.
我将默认的jta数据源JNDI名称保留为AppDataSource
本身.
我实际上对JTA和非JTA感到困惑.他们有什么区别?
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 AppDataSource
itself.
I'm actually confused regarding JTA and non-JTA. What differentiates them?
我生成了实体,并在bean中创建了一个EntityTransaction
对象.
调用persist()
和commit()
方法时,出现错误:
I generated the entities and created an EntityTransaction
object in the bean.
Upon calling the persist()
and commit()
methods, I get an error:
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.
openjpa在这里如何出现?
How does openjpa comes into picture here?
到目前为止,我的persistence.xml
仅包含实体类名称.
As of now, my persistence.xml
contains the entity class names only.
如何使它使用默认的appserver值作为数据源.否则,如何在persistence.xml
中提供jpa provider的详细信息和数据源的详细信息?
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
?
请提供您的输入.
推荐答案
这个问题实际上涉及许多概念,我将不对其进行详细介绍.有关更详尽的详细信息,建议阅读JPA 1.0规范的整个第5章-实体管理器和持久性上下文(以及此答案末尾提到的其他相关部分).我宁愿尝试描述Java EE环境中最常见的情况.
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 Contexts of 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.
在EJB环境中,通常会使用:
Within an EJB environment, one would typically use:
- 容器管理的实体管理器(必须是JTA实体管理器)
-
无状态会话Bean(SLSB)中的
- 事务作用域 有状态会话Bean(SFSB)中的
- 扩展
- A container-managed entity manager (must be a JTA entity manager)
- transaction-scoped in Stateless Session Beans (SLSB)
- extended in Stateful Session Beans (SFSB)
这是如何设置JTA实体管理器的
persistence.xml
,该管理器使用具有JNDI名称java:comp/env/jdbc/AppDataSource
的数据源,而OpenJPA是WebSphere所使用的持久性提供程序:Here is how a
persistence.xml
for a JTA entity manager that uses a data source with a JNDI namejava:comp/env/jdbc/AppDataSource
can be set up, OpenJPA being the persistence provider used by 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.
以下是SLSB(使用容器管理的交易)如何获取注入的容器管理的实体管理器的方法:
And here is how a SLSB (using Container Managed Transactions) could get a container managed Entity Manager injected:
@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; } ... }
仅此而已.容器对应用程序透明地管理实体管理器的生命周期(没有createEM/close),实体管理器参与由容器管理的JTA事务(没有明确的开始/提交).
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).
如前所述,我只是在摸摸表面,我的目标是以某种方式使您走上正确的道路.为了进一步讲解,我建议您拿一本书(例如EJB3 in Action).同时,下面的参考文献将是一个很好的阅读.
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.
- 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数据源,非jta数据源"
- 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"
这篇关于IBM Websphere JPA配置-如何更新persistence.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!