在EJB 3.0 學習筆記——準備工作中只是簡單的搭好了EJB3.0開發的基本環境,之
后就可以開發最簡單的Session Bean了,我感興趣的還是Entity Bean,所以接下來
我想先試驗一下Entity Bean。
?
一、在JBoss中配置好Data Source
我使用的是MySQL數據庫,所以首先將MySQL的JDBC驅動復制到
jboss-4.0.3SP1\server\all\lib目錄,然后將jboss-4.0.3SP1\docs\examples\jca
下的mysql-ds.xml作出適當修改后復制到jboss-4.0.3SP1\server\all\deploy目錄
下,這是我修改后的mysql-ds.xml文件:
<?
xml?version="1.0"?encoding="UTF-8"
?>
<!--
?$Id:?mysql-ds.xml,v?1.3.2.1?2004/12/01?11:46:00?schrouf?Exp?$?
-->
<!--
??Datasource?config?for?MySQL?using?3.0.9?available?from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<
datasources
>
??
<
local-tx-datasource
>
????
<
jndi-name
>
MySqlDS
</
jndi-name
>
????
<
connection-url
>
jdbc:mysql://localhost:3306/test
</
connection-url
>
????
<
driver-class
>
com.mysql.jdbc.Driver
</
driver-class
>
????
<
user-name
>
test
</
user-name
>
????
<
password
></
password
>
????
<
exception-sorter-class-
name
>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</
excepti
on-sorter-class-name
>
????
<!--
?sql?to?call?when?connection?is?created
????<new-connection-sql>some?arbitrary?sql</new-connection-sql>
??????
-->
????
<!--
?sql?to?call?on?an?existing?pooled?connection?when?it?is?obtained?
from?pool?
????<check-valid-connection-sql>some?arbitrary?sql</check-valid-
connection-sql>
??????
-->
????
<!--
?corresponding?type-mapping?in?the?standardjbosscmp-jdbc.xml?
(optional)?
-->
????
<
metadata
>
???????
<
type-mapping
>
mySQL
</
type-mapping
>
????
</
metadata
>
??
</
local-tx-datasource
>
</
datasources
>
這樣之后,JBoss下的MySQL Data Source配置完成。
二、創建數據庫并編寫Entity Bean代碼
create table book(
id int not null auto_increment primary key,
title varchar(20) not null,
author varchar(40) not null
);
新建Java Project,導入User Library:EJB3_JBoss,以下是類代碼。
//
Book.java
package
?ejb.bean.entity;
import
?java.io.Serializable;
import
?javax.persistence.Entity;
import
?javax.persistence.GeneratedValue;
import
?javax.persistence.GenerationType;
import
?javax.persistence.Id;
import
?javax.persistence.Table;
@Entity
@Table(name
=
"
book
"
)
public
?
class
?Book?
implements
?Serializable?{
?
/**
??*?
??
*/
?
private
?
static
?
final
?
long
?serialVersionUID?
=
?
1L
;
?
private
?Integer?id;?
?
private
?String?title;?
?
private
?String?author;
?
?
public
?Book()?{??
??
super
();?
?}?
?
public
?Book(Integer?id,?String?title,?String?author)?{?
??
super
();??
??
this
.id?
=
?id;??
??
this
.title?
=
?title;??
??
this
.author?
=
?author;?
??}
?@Override
?
public
?String?toString()?{?
??
??
return
?
"
Book:?
"
?
+
?getId()?
+
?
"
?Title?
"
?
+
?getTitle()?
+
?
"
?
Author?
"
?+?getAuthor();?
?}
?
public
?String?getAuthor()?{
??
return
?author;
?}
?
?@Id?@GeneratedValue(strategy
=
GenerationType.AUTO)
?
public
?Integer?getId()?{
??
return
?id;
?}
?
public
?String?getTitle()?{
??
return
?title;
?}
?
public
?
void
?setAuthor(String?author)?{
??
this
.author?
=
?author;
?}
?
public
?
void
?setId(Integer?id)?{
??
this
.id?
=
?id;
?}
?
public
?
void
?setTitle(String?title)?{
??
this
.title?
=
?title;
?}
}
三、編寫一個簡單的Stateless Session Bean 并進行測試
//
BookTestLocal.java
package
?ejb.bean.entity;
import
?javax.ejb.Local;
@Local
public
?
interface
?BookTestLocal?{
?
public
?
void
?test();
}
//
BookTestRemote.java
package
?ejb.bean.entity;
import
?javax.ejb.Remote;
@Remote
public
?
interface
?BookTestRemote?{
?
?
public
?
void
?test();
}
//
BookTestBean.java
package
?ejb.bean.entity;
import
?javax.ejb.Stateless;
import
?javax.persistence.EntityManager;
import
?javax.persistence.PersistenceContext;
@Stateless
public
?
class
?BookTestBean?
implements
?BookTestLocal,?BookTestRemote?{
?@PersistenceContext
?EntityManager?em;
?
public
?
void
?test()?{
??
//
?TODO?Auto-generated?method?stub
??Book?book?
=
?
new
?Book(
null
,?
"
My?first?bean?book
"
,?
"
Sebastian
"
);
??em.persist(book);
?}
}
?
//
Client.java
package
?ejb.client.entity;
import
?ejb.bean.entity.
*
;
import
?javax.naming.InitialContext;
/**
?*?Comment
?*
?*?
@author
?<a?href="mailto:bill@jboss.org">Bill?Burke</a>
?*?
@version
?$Revision:?1.1.6.7?$
?
*/
public
?
class
?Client
{
???
public
?
static
?
void
?main(String[]?args)?
throws
?Exception
???{
??????InitialContext?ctx?
=
?
new
?InitialContext();
??????BookTestRemote?book?
=
?(BookTestRemote)?ctx.lookup
(
"
BookTestBean/remote
"
);
?????
???
??????book.test();
??????
??????System.out.println(
"
test?successful!?
"
);
???}
}
三、其他文件
將jboss-EJB-3.0_RC5-PFD\docs\tutorial中的找到的jndi.properties、log4j.xml、builder.xml等復制到當前工程中,其中builder.xml需要修改。
//builder.xml
<?
xml?version="1.0"
?>
<!--
?
=======================================================================?
-->
<!--
?JBoss?build?file?????????????????????????????????????????????????????
??
-->
<!--
?
=======================================================================?
-->
<
project?
name
="JBoss"
?default
="ejbjar"
?basedir
="."
>
???
<
property?
file
="../local.properties"
?
/>
???
<
property?
environment
="env"
/>
???
<
property?
name
="src.dir"
?value
="${basedir}/src"
/>
???
<
property?
name
="jboss.home"
?value
="E:/Programming/Servers/jboss-
4.0.3SP1/"
/>
???
<
property?
name
="jboss.server.config"
?value
="all"
/>
???
<
property?
name
="build.dir"
?value
="${basedir}/build"
/>
???
<
property?
name
="build.classes.dir"
?value
="${build.dir}/classes"
/>
???
<!--
?Build?classpath?
-->
???
<
path?
id
="classpath"
>
??????
<!--
?So?that?we?can?get?jndi.properties?for?InitialContext?
-->
??????
<
pathelement?
location
="${basedir}"
/>
??????
<
fileset?
dir
="${jboss.home}/lib"
>
?????????
<
include?
name
="**/*.jar"
/>
??????
</
fileset
>
??????
<
fileset?
dir
="${jboss.home}/server/${jboss.server.config}/lib"
>
?????????
<
include?
name
="**/*.jar"
/>
??????
</
fileset
>
??????
<
fileset?
dir
="${jboss.home}/server/
${jboss.server.config}/deploy/ejb3.deployer"
>
?????????
<
include?
name
="*.jar"
/>
??????
</
fileset
>
??????
<
fileset?
dir
="${jboss.home}/server/
${jboss.server.config}/deploy/jboss-aop-jdk50.deployer"
>
?????????
<
include?
name
="*.jar"
/>
??????
</
fileset
>
??????
<
pathelement?
location
="${build.classes.dir}"
/>
???
</
path
>
???
<
property?
name
="build.classpath"
?refid
="classpath"
/>
???
<!--
?
===================================================================?
-->
???
<!--
?Prepares?the?build?directory??????????????????????????????????????
??
-->
???
<!--
?
===================================================================?
-->
???
<
target?
name
="prepare"
>
??????
<
mkdir?
dir
="${build.dir}"
/>
??????
<
mkdir?
dir
="${build.classes.dir}"
/>
???
</
target
>
???
<!--
?
===================================================================?
-->
???
<!--
?Compiles?the?source?code??????????????????????????????????????????
??
-->
???
<!--
?
===================================================================?
-->
???
<
target?
name
="compile"
?depends
="prepare"
>
??????
<
javac?
srcdir
="${src.dir}"
?????????destdir
="${build.classes.dir}"
?????????debug
="on"
?????????deprecation
="on"
?????????optimize
="off"
?????????includes
="**"
>
?????????
<
classpath?
refid
="classpath"
/>
??????
</
javac
>
???
</
target
>
???
<
target?
name
="ejbjar"
?depends
="compile"
>
??????
<
jar?
jarfile
="build/tutorial.jar"
>
?????????
<
fileset?
dir
="${build.classes.dir}"
>
????????????
<
include?
name
="**/*.class"
/>
?????????
</
fileset
>
????????
<
fileset?
dir
="."
>
???????????
<
include?
name
="META-INF/persistence.xml"
/>
????????
</
fileset
>
??????
</
jar
>
??????
<
copy?
file
="build/tutorial.jar"
?todir
="${jboss.home}/server/
${jboss.server.config}/deploy"
/>
???
</
target
>
???
<
target?
name
="run.stateless"
?depends
="ejbjar"
>
??????
<
java?
classname
="ejb.client.stateless.Client"
?fork
="yes"
?dir
="."
>
?????????
<
classpath?
refid
="classpath"
/>
??????
</
java
>
???
</
target
>
?
?
<
target?
name
="run.stateful"
?depends
="ejbjar"
>
???????
<
java?
classname
="ejb.client.stateful.Client"
?fork
="yes"
?
dir
="."
>
??????????
<
classpath?
refid
="classpath"
/>
???????
</
java
>
?
</
target
>
?
?
<
target?
name
="run.timer"
?depends
="ejbjar"
>
???????
<
java?
classname
="ejb.client.timer.Client"
?fork
="yes"
?
dir
="."
>
??????????
<
classpath?
refid
="classpath"
/>
???????
</
java
>
?
</
target
>
?
?
<
target?
name
="run.entity"
?depends
="ejbjar"
>
????????
<
java?
classname
="ejb.client.entity.Client"
?
fork
="yes"
?dir
="."
>
???????????
<
classpath?
refid
="classpath"
/>
????????
</
java
>
??
</
target
>
?
???
<!--
?
===================================================================?
-->
???
<!--
?Cleans?up?generated?stuff?????????????????????????????????????????
??
-->
???
<!--
?
===================================================================?
-->
???
<
target?
name
="clean.db"
>
??????
<
delete?
dir
="${jboss.home}/server/
${jboss.server.config}/data/hypersonic"
/>
???
</
target
>
???
<
target?
name
="clean"
>
??????
<
delete?
dir
="${build.dir}"
/>
??????
<
delete?
file
="${jboss.home}/server/
${jboss.server.config}/deploy/tutorial.jar"
/>
???
</
target
>
</
project
>
最后,在工程目錄下新建目錄META-INF,在目錄META-INF新建persistence.xml文件,以下是文件內容:
<?
xml?version="1.0"?encoding="UTF-8"
?>
<
persistence
>
???
<
persistence-unit?
name
="test"
>
??????
<
jta-data-source
>
java:/MySqlDS
</
jta-data-source
>
??????
<
properties
>
???????
<
property?
name
="hibernate.dialect"
?
value
="org.hibernate.dialect.MySQLDialect"
/>
???????
????????
<
property?
name
="hibernate.hbm2ddl.auto"
?value
="update"
/>
??????
</
properties
>
???
</
persistence-unit
>
</
persistence
>
四、運行測試
run as->ant build后,選擇運行目標為run.entity->run。
運行結果:
Buildfile:?D:\Programs\Java\EclipseWork\EJB3\build.xml
prepare:
compile:
ejbjar:
run.entity:
?????
[
java
]
?test?successful!
BUILD?SUCCESSFUL
Total?time:?
9
?seconds
MySQL中
select
?
*
?
from
?book;
id????????title?????????????????????author
1
????????My?first?bean?book?????????Sebastian
已經成功寫入。
?
?
?