EJB3的持久化机制与Hibernate很相似:
环境:
服务器:JBOSS5.0
数据库:MySQL5.0
1.建立数据源
首先在jboss-5.0.0.GA\server\default\deploy下,建立一个用于连接数据库的数据源。
建立一个XML文件,名字为:mysql-ds.xml,其内容如下:
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb3inaction</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>200596</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>
当然不同的数据库,配置的方法不一样,具体可参考jboss-5.0.0.GA\docs\examples\jca下相应的XML文件。
必须把MYSQL的JDBC驱动复制到jboss-5.0.0.GA\server\default\lib下。
2.创建持久化单元
在src/META-INF目录下,创建一个XML文件persitence.xml,内容如下:
<?xml version="1.0"?>
<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_1_0.xsd" version="1.0">
<persistence-unit name="mysqlpersistence">
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
3.建立一实体类:
package com.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="userinfo")
public class UserInfo implements Serializable {
private static final long serialVersionUID = -3200018567382795556L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userId;
@Column(name="user_name",length=32)
private String username;
@Column(name = "password",nullable=false)
private String password;
public Integer getUserId() {
return userId;
}
public String getUsername() {
return username;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注意关于@Column注解既可用于属性,也可用于字段。即可这样用:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getUserId() {
return userId;
}
但不能同时在字段上和属性上用此注解,即不能在
private Integer userId;
public Integer getUserId() {
return userId;
}
同时用。但可以不同的字段可以这样用,即如果UserId的注解用在getUserId()上,其它字段可用在字段上,但是实际起作用的属性的注解,即关于字段的注解将被忽略。
4.建立Session接口与Session Bean类
package com.dao;
import javax.ejb.Remote;
import com.entity.UserInfo;
@Remote
public interface IUserDao {
UserInfo getUserInfo(int userId);
void insertUser(UserInfo user);
}
package com.dao;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.entity.UserInfo;
@Stateless
public class UserDaoImp implements IUserDao{
@PersistenceContext(unitName="mysqlpersistence")
EntityManager entityManager;
public UserInfo getUserInfo(int userId) {
UserInfo userInfo= entityManager.find(UserInfo.class, userId);
return userInfo;
}
public void insertUser(UserInfo user) {
entityManager.persist(user);
}
}
5.将实体Bean与SessionBean,还有META-INF文件夹打包成一个jar文件,部署在 jboss-5.0.0.GA\server\default\deploy,基本上就可以了。
6.为了测试刚才的EJB 是否能进行持久化。
在src下建立一属性文件,用于JNDI的查找,文件名为:jndi.properties,内容如下:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
再建立一测试类EjbPersistenceTest.java,内容如下:
package test;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.dao.IUserDao;
import com.entity.UserInfo;
public class EjbPersistenceTest {
public static void main(String[] args) throws NamingException {
// TODO Auto-generated method stub
Context initialContext = new InitialContext();
IUserDao user = (IUserDao) initialContext.lookup("UserDaoImp/remote");
//先进行数据插入
UserInfo userInfo = new UserInfo();
userInfo.setUsername("javait");
userInfo.setPassword("2008");
user.insertUser(userInfo);
//再通过ID从数据库进行检索
UserInfo userInfo = user.getUserInfo(1);
System.out.println("username="+userInfo.getUsername());
}
}
正常情况下,会输入username=javait,一个EJB的持久化过程就完成了。