2016-11-29 57 views

回答

0

只需添加<jta-data-source>java:/ExampleDS</jta-data-source>即可將數據源​​jndi-name提供給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:

<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中引用的數據源ExampleDS的例子

@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); 
    } 
... 
} 
+0

我應該在我的打包MySQL驅動程序連接器? –

+0

最好的方法是在JBoss AS中配置一個模塊。要做到這一點,只需遵循該指南:https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6/html/Administration_and_Configuration_Guide/Install_a_JDBC_Driver_as_a_Core_Module1.html –

相關問題