1.在jboss的部署目录新增mysql-ds.xml,这个文件在jboss安装目录可以找到
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://127.0.0.1:3306/student</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>123456</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
2.在EJB项目中新增JPA支持
这个操作一般让程序帮我们自动建立就ok,如下所示
还有一个问题需要注意就是上面的java版本,这个一般和java_home的版本一致
到这一步我们要在eclipse里创建数据源
接下来我们选择hibernate为jpa规范,之后eclipse会为我们自动配置jpa的配置文件,配置结束后文件可能是这样的
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" 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"> <persistence-unit name="TestTransaction"> <class>org.lxh.model.MUser</class> <properties> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123456"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/student"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> </properties> </persistence-unit> </persistence>
遗憾的是把项目部署到jboss会出现错误,因为jboss数据源已经配置好但是jpa配置文件没有使用jboss的数据源,且没有配置事务处理器,可以参考下面的配置
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" 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"> <persistence-unit name="TestTransaction"> <jta-data-source>java:MySqlDS</jta-data-source> <class>org.lxh.model.MUser</class> <properties> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> </properties> </persistence-unit> </persistence>
jta-data-source结点配置的就是jboss数据源的jndi,properties里配置jpa的事务处理器
3接下来在相应的包里创建实体bean就ok了
在相应包上右击,从中可以选择创建实体bean,根据提示实体Bean就能创建成功
package org.lxh.model; import java.io.Serializable; import javax.persistence.*; /** * The persistent class for the m_user database table. * */ @Entity @Table(name="m_user") @NamedQuery(name="MUser.findAll", query="SELECT m FROM MUser m") public class MUser implements Serializable { private static final long serialVersionUID = 1L; @Id private int id; private String address; private String name; private String password; public MUser() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
4.在EJB业务Bean里取得EntityManager
这个和依赖注入的原理基本类似,我们注入ejb可以使用@EJB注解,那么EntityManager的注入也是类似的
@PersistenceContext(unitName="TestTransaction") private EntityManager em; public void updateUser() throws Exception { MUser m=em.find(MUser.class, 3); m.setName("刘惜君XX"); em.merge(m); }
上面的前两句代码就实现了EntityManager的注入那后面就可以进行相关操作了。要明白的一点的就是jpa操作数据库不需要显式的开启事务提交事务