EJB3.0 Hibernate JPA持久化配置

本文详细介绍如何在JBoss 4.2.3.GA环境下配置MySQL数据源,并结合Hibernate 4.2.13.Final实现持久层管理。文章包括部署目录下mysql-ds.xml的设置方法及ejbjar项目中persistence.xml的具体配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统要求

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的。

转载于:https://my.oschina.net/DreamZhong/blog/282672

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值