EJB3.0 Hibernate JPA持久化配置

系统要求

1. jboss-4.2.3.GA

2. hibernate-entitymanager 4.2.13.Final

3. MYSQL

配置步骤

1. 在deploy目录下配置mysql-ds.xml


<?xml version="1.0" encoding="UTF-8"?>
<datasources>
   <local-tx-datasource>
      <!-- The jndi name of the DataSource, it is prefixed with java:/ -->
      <!-- Datasources are not available outside the virtual machine -->
      <jndi-name>MysqlDS</jndi-name>
      <connection-url>jdbc:mysql://127.0.0.1:3306/template?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</connection-url>
      <driver-class>om.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      <password>root</password>
   </local-tx-datasource>
</datasources>



2. 在ejb jar项目下配置persistence.xml



<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"
             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">
    <persistence-unit name="template">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/MysqlDS</jta-data-source>
        <class>com.icode.jejb.core.StandardUser</class>
        <properties>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.jdbc.fetch_size" value="18"/>
            <property name="hibernate.jdbc.batch_size" value="100"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>



3. 在项目中通过@PersistenceContext(unitName = "template")注解来注入entityManager


附:如果EJB Beans之间有依赖关系,可以通过@EJB(name = "UserDaoImpl")注解来注入依赖组件;如果要对数据库操作进行测试,可以再重新创建一个本地的PersistenceUnit,因为上述PersistenceUnit是从JBoss容器中获取DataSource的。

你可能感兴趣的:(EJB3.0 Hibernate JPA持久化配置)