的持久性单元
我有一个jboss as,在此服务器上有一个独立的.xml文件,那里也有几个属性,我的数据源也是如此,因此如何在standalone.xml文件中组合数据源我想添加到EJB?
只需添加 <jta-data-source>java:/ExampleDS</jta-data-source>
将您的dataSource jndi-name提供到persistence.xml。
dataSource的示例:
<datasource jndi-name="java:/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
persistence.xml引用数据源检查的示例:
<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_2_0.xsd"
version="2.0">
<persistence-unit name="example">
<jta-data-source>java:/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
示例在您的EJB3中注入持久性单元:
@Stateless
public class MyEJB {
@PersistenceContext(unitName="example") protected EntityManager entityManager;
public void createEmployee(String fName, String lName) {
Employee employee = new Employee();
employee.setFirstName(fName);
employee.setLastName(lName);
entityManager.persist(employee);
}
...
}