EJB3-使用Eclipse和JBoss开发和部署Entity Bean

作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!

环境 | Enviroment

  • Ubuntu 10.10
  • Eclipse 3.6.1
  • JBoss GA 5.1.0

提示:EJB项目的建立参考我的另一篇文章《EJB3-使用Eclipse和JBoss开发和部署》。

建立表结构和对应的EntityBean | Create table structure and the corresponding EntityBean

1、表结构如下:

1
2
3
4
5
6
7
8
9
create table ACCOUNT (
USER_ID VARCHAR2(11) not null,
USER_NAME VARCHAR2(10) not null,
USER_PASSWD VARCHAR2(20) not null
);
alter table ACCOUNT
add constraint PK_ACCOUNT_USER_ID primary key (USER_ID)
using index;

2、在数据库中建立表,并初始化数据。如下图所示:
Account表的初始化数据

3、建立表结构对应的POJO。完整的代码的清单如下:
1)de>Accountde>.java的源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 建立时间:2011-3-14
*/
package cn.aofeng.ejb3.domain;
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.SequenceGenerator;
import javax.persistence.Table;
/**
* 表Account对应的POJO.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
@Entity
@Table(name="ACCOUNT")
@SequenceGenerator(name="user_id",sequenceName="SEQ_ACCOUNT")
public class Account implements Serializable {
private static final long serialVersionUID = 6116303394942546611L;
@Id
@Column(name="USER_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="user_id")
private Long userId;
@Column(name="USER_NAME")
private String userName;
@Column(name="USER_PASSWD")
private String userPasswd;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPasswd() {
return userPasswd;
}
public void setUserPasswd(String userPasswd) {
this.userPasswd = userPasswd;
}
}

建立DAO | Create DAO

1、DAO完整的源代码清单如下:
1)远程接口AccountDaoRemote.java的源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 建立时间:2011-3-14
*/
package cn.aofeng.ejb3;
import javax.ejb.Remote;
import cn.aofeng.ejb3.domain.Account;
/**
* 表Account DAO远程接口定义.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
@Remote
public interface AccountDaoRemote {
/**
* 根据账号名称查询对应的账号信息.
*
* @param userName 账号名称.
* @return 如果有符合条件的记录,返回一个{@link cn.aofeng.ejb3.domain.Account}实例;如果没有符合条件的记录返回null.
*/
Account selectByUserName(String userName);
}

2)本地接口AccountDaoLocale.java的源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 建立时间:2011-3-14
*/
package cn.aofeng.ejb3;
import javax.ejb.Local;
import cn.aofeng.ejb3.domain.Account;
/**
* 表Account DAO本地接口定义.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
@Local
public interface AccountDaoLocal {
/**
* 根据账号名称查询对应的账号信息.
*
* @param userName 账号名称.
* @return 如果有符合条件的记录,返回一个{@link cn.aofeng.ejb3.domain.Account}实例;如果没有符合条件的记录返回null.
*/
Account selectByUserName(String userName);
}

3)DAO类AccountDaoEJBImpl.java的源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 建立时间:2011-3-14
*/
package cn.aofeng.ejb3;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import cn.aofeng.ejb3.domain.Account;
/**
* 账号表DAO EJB3实现.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
@Stateless(name="AccountDao")
public class AccountDaoEJBImpl implements AccountDaoRemote, AccountDaoLocal {
@PersistenceContext(unitName="jboss_ejb3")
private EntityManager _em;
/**
* Default constructor.
*/
public AccountDaoEJBImpl() {
}
/*
* @see cn.aofeng.ejb3.AccountDaoRemote#selectByUserName(java.lang.String)
* @see cn.aofeng.ejb3.AccountDaoLocale#selectByUserName(java.lang.String)
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Account selectByUserName(String userName) {
Query query = _em.createQuery("from Account a where a.userName=:userName");
query.setParameter("userName", userName);
Account result = (Account) query.getSingleResult();
return result;
}
}

在JBoss中部署EJB项目 | Deploy EJB Project in JBoss

1、启动JBoss,在浏览器中输入http://localhost:8080/console ,进入控制台,建立数据源JBOSS_EJB3_DS。
JBoss AS Administration Console

2、在”Servers”中的“JBoss v5.0 localhost”中点击右键,选择菜单中的“Add and Remove”。
Add/Remove Server

3、选择“Available”下拉框中选择项目“JBossEJB3“,点击“Add >”按钮,将项目添加到“Configured”下拉框中。
Add/Remove Server
点击“Finish”按钮完成添加。

4、如下图所示:项目JBossEJB3已经成功部署到JBoss中。
Deploy Finish

建立EJB3客户端| Create EJB3 Client

1、在源代码文件夹”src/main/resources/META-INF”下建立持久化配置文件:persistence.xml。内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<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="jboss_ejb3" transaction-type="JTA">
<jta-data-source>JBOSS_EJB3_DS</jta-data-source>
<properties>
<property name="org.hibernate.hbm2ddl" value="none"></property>
</properties>
</persistence-unit>
</persistence>

2、在源代码文件夹”test/main/java”下建立EJB3 Client类:AccountDaoEJBImplTest。源代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 建立时间:2011-3-14
*/
package cn.aofeng.ejb3;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.aofeng.ejb3.domain.Account;
/**
* {@link cn.aofeng.ejb3.AccountDaoEJBImpl}的单元测试代码.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
public class AccountDaoEJBImplTest extends RemoteEjbTest {
private AccountDaoRemote _dao;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
_dao = (AccountDaoRemote) _context.lookup("AccountDao/remote");
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
_dao = null;
}
/**
* Test method for {@link cn.aofeng.ejb3.AccountDaoEJBImpl#selectByUserName(java.lang.String)}.
*/
@Test
public void testQueryUser() {
Account testPtlUser = _dao.selectByUserName("aofeng");
Assert.assertNotNull(testPtlUser);
Assert.assertEquals("aofeng", testPtlUser.getUserPasswd());
}
}

3、运行AccoutDaoEJBImplTest类,结果如下图所示:
在单元测试中调用EJB