
EJB3.0 step by step<course 1>
|
Wangwj/scttsc 建立一个entity bean,注意它的annotation,@Entity和@Table,它告诉容器这是一个数据库映射类。 package com.titan.domain; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.Id; @Entity @Table(name="TEST_EMPLOYEE") public class Test_employee { private int id; private String name; private int age; private String address; @Id @Column(name="ID") public int getId() { return id; } public void setId(int pk) { id = pk; } @Column(name="NAME") public String getName() { return name; } public void setName(String str) { name = str; } @Column(name="AGE") public int getAge() { return age; } public void setAge(int ag) { age = ag; } @Column(name="ADDRESS") public String getAddress() { return address; } public void setAddress(String addr) { address = addr; } } Java persistence要求一个persistence.xml的XML部署描述文件,里面是一些基本配置信息。 <?xml version="1.0" encoding="UTF-8"?> <persistence> <persistence-unit name="titan"> <jta-data-source>java:/OracleDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence> 开发session bean,定义远程接口和实现业务逻辑 该接口定义了两个方法,一个是创建对象,一个是寻找对象。 package com.titan.travelagent; import javax.ejb.Remote; import com.titan.domain.Test_employee; @Remote public interface EmployeeRemote { public void createEmployee(Test_employee em); public Test_employee findTest_employee(int pKey); } 实现 package com.titan.travelagent; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import com.titan.domain.Test_employee; @Stateless public class EmployeeBean implements EmployeeRemote{ @PersistenceContext(unitName="titan") private EntityManager manager; public void createEmployee(Test_employee em){ manager.persist(em); } public Test_employee findTest_employee(int pKey){ return manager.find(Test_employee.class, pKey); } } Step4.build.xml解析 在eclipse里面右键点击build.xml,选择run as ->ant build…(第三个),注意到它有4个任务,默认的任务是编译和部署,运行一次后,就编译成功。Prepare任务是准备目录。 <?xml version="1.0"?> <project name="JBoss" default="ejbjar" basedir="."> <property environment="env"/> <property name="src.dir" value="${basedir}/src"/> <property name="src.resources" value="${basedir}/resources"/> <property name="jboss.home" value="${env.JBOSS_HOME}"/> <property name="build.dir" value="${basedir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <!-- Build classpath --> <path id="classpath"> <fileset dir="${jboss.home}/server/default/lib"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/server/default/deploy/ejb3.deployer"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/server/default/deploy/jboss-aop-jdk50.deployer"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/lib"> <include name="*.jar"/> </fileset> <pathelement location="${build.classes.dir}"/> <pathelement location="${basedir}/client-config"/> </path> <property name="build.classpath" refid="classpath"/> <target name="prepare" > <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> </target> <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/titan.jar"> <fileset dir="${build.classes.dir}"> <include name="com/titan/domain/*.class"/> <include name="com/titan/travelagent/*.class"/> </fileset> <fileset dir="${src.resources}/"> <include name="META-INF/persistence.xml"/> </fileset> </jar> <copy file="build/titan.jar" todir="${jboss.home}/server/default/deploy"/> </target> <target name="run.client" depends="ejbjar"> <java classname="com.titan.clients.Client" fork="yes" dir="."> <classpath refid="classpath"/> </java> </target> </project> Step5.测试代码 package com.titan.clients; import com.titan.travelagent.EmployeeRemote; import com.titan.domain.Test_employee; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; public class Client { public static void main(String [] args) { try { Context jndiContext = getInitialContext(); //查找JNDI对象 Object ref = jndiContext.lookup("EmployeeBean/remote"); EmployeeRemote dao = (EmployeeRemote)ref; //创建一个em对象 Test_employee em = new Test_employee(); em.setId(1); em.setAge(27); em.setName("valen won"); em.setAddress("north 2 building software incubation center south-line chengdu"); //保存 dao.createEmployee(em); System.out.println("let's find the entity that we just put in the database."); Test_employee em2 = dao.findTest_employee(1); System.out.println(em2.getName()); System.out.println(em2.getAddress()); System.out.println(em2.getAge()); } catch (javax.naming.NamingException ne) { ne.printStackTrace(); } } public static Context getInitialContext() throws javax.naming.NamingException { return new javax.naming.InitialContext(); } } Step6.测试 启动jboss,进入D:\jboss-4.2.2.GA\bin,运行run.bat默认启动default 用ant的run.client任务结果如下 Buildfile: D:\BEATRAINING\Eclipse\workspace\ejb41\build.xml prepare: compile: ejbjar: run.client: [java] let's find the entity [java] valen won [java] north 2 building software incubation center south-line chengdu [java] 27 BUILD SUCCESSFUL Total time: 2 seconds 现在数据库中已经有一条记录,如果你再运行一次,就会发现ORA-00001: unique constraint (APS2.SYS_C0019139) violated。 不要慌张,改掉client里面的id就可以再运行一次。 OK,第一个EJB例子完成了。 Continue... 完整源代码下载,附件中。ejb3 ejb41.rar |
一共有 0 条评论