EJB3.0学习笔记之EntityBean

一、在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

已经成功写入。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值