系统要求
1.jboss-4.2.3.GA
2.hibernate-entitymanager 4.2.13.Final
3.MYSQL
配置步骤
1. 在deploy目录下配置mysql-ds.xml
<?xmlversion="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&useUnicode=true&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
<?xmlversion="1.0"?>
<persistencexmlns="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-unitname="template">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MysqlDS</jta-data-source>
<class>com.icode.jejb.core.StandardUser</class>
<properties>
<propertyname="hibernate.max_fetch_depth"value="3"/>
<propertyname="hibernate.hbm2ddl.auto"value="validate"/>
<propertyname="hibernate.jdbc.fetch_size"value="18"/>
<propertyname="hibernate.jdbc.batch_size"value="100"/>
<propertyname="hibernate.show_sql"value="true"/>
<propertyname="hibernate.format_sql"value="true"/>
</properties>
</persistence-unit>
</persistence>
3. 在项目中通过@PersistenceContext(unitName = "template")注解来注入entityManager
附:如果EJB Beans之间有依赖关系,可以通过@EJB(name = "UserDaoImpl")注解来注入依赖组件;如果要对数据库操作进行测试,可以再重新创建一个本地的PersistenceUnit,因为上述PersistenceUnit是从JBoss容器中获取DataSource的。
--转自