This is a very simple example and I'm sure can use alot of work:

Deploying Entity Beans:

First I would follow the orion primer located at
http://www.zdnerd.demon.nl/orion-primer and set up a session bean.  Most
of the instructions following assume you have this application setup
with the same directory structure.  We going to create a bean with 1
field: long id (primary key) .

1) Create your bean (using the ejbmaker tool in the orion directory will
do most of the work for you.  Use java �jar orion.jar  in the orion
directory to start it).  Let�s call them TestEnt (Remote interface),
TestEntEJB (Bean), TestEntHome (Home interface), TestEntPK (Primary
key).  The source files should be in your
\orion-primer\src\java\hello\ejb\ (assuming they are in a package
hello.ejb.) directory following the orion-primer tutorial.  We will make
a servlet called TestEntServlet in \orion-primer\src\java\hello\web\
(assuming they are in a package hello.web.) to create the TestEnt
instance.

Below is the source code for all five classes:

TestEnt.class (remote interface):

import java.rmi.RemoteException;
import javax.ejb.*;

public interface TestEnt extends EJBObject
{
 public long getId() throws RemoteException;
}

TestEntBean.class (Bean):

package hello.ejb;

import java.rmi.*;
import javax.ejb.*;

public class TestEntBean implements EntityBean {
  private EntityContext entityContext;
  public long id;


  public void ejbActivate() throws RemoteException {
  }

  public void ejbLoad() throws RemoteException {
  }

  public void ejbPassivate() throws RemoteException {
  }

  public void ejbRemove() throws RemoteException, RemoveException {
  }

  public void ejbStore() throws RemoteException {
  }

  public void setEntityContext(EntityContext context) throws
RemoteException {
    entityContext = context;
  }

  public void unsetEntityContext() throws RemoteException {
    entityContext = null;
  }

  public TestEntPK ejbCreate(long id) throws CreateException  {
  this.id = id;
  return null; // Return null when using CMP
 }

 public void ejbPostCreate(long id)  {
 }

  public void setId(long newId) {
    id = newId;
  }

  public long getId() {
    return id;
  }
}


TestEntHome.class (Home interface):

import javax.ejb.*;
import java.rmi.RemoteException;

public interface TestEntHome extends EJBHome
{
 public TestEnt create(long id) throws CreateException, RemoteException;

 public TestEnt findByPrimaryKey(long key) throws RemoteException,
FinderException;
}

TestEntPK.class (Primary Key):

package hello.ejb;

public class TestEntPK {

  public TestEntPK() {
  }

  public long id;
}

TestEntServlet.class (Servlet):

package hello.web;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import hello.ejb.TestEnt;
import hello.ejb.TestEntHome;

public class TestEntServlet extends HttpServlet {

   // constructor
   public TestEntServlet() {
      super();
      trace("<init>");
   }

   // A reference to the remote `Hello' object
   protected TestEnt _testEnt;

   // Initializes this servlet
   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      trace("init");


   }

   // Handles the HTTP GET request
   public void doGet(HttpServletRequest request, HttpServletResponse
response)
   throws ServletException, IOException {
      trace("doGet");

      // Get the initial JNDI context using our settings
      Context context;
      try {
         context = new InitialContext();
      }
      catch (Throwable exception) {
         throw new ServletException(
            "Unable to get initial JNDI context: " +
exception.toString());
      }

      // Get a reference to the TestEnt home interface
      TestEntHome testEntHome;
      try {
         Object boundObject =
context.lookup("java:comp/env/ejb/TestEntHome");
         testEntHome = (TestEntHome)
PortableRemoteObject.narrow(boundObject,TestEntHome.class);
      }
      catch (Throwable exception) {
         exception.printStackTrace();
         throw new ServletException(
            "Unable to get home interface here: " +
exception.toString());
      }

      ServletOutputStream out = response.getOutputStream();

      response.setContentType("text/html");

      // Get a reference to a TestEnt instance
      System.out.println("about to create TestEnt bean");
      try {
         _testEnt =
testEntHome.create(Long.parseLong(request.getParameter("ID")));
         System.out.println("created testEnt bean");
         out.println("<HTML><BODY bgcolor=\"#FFFFFF\">");
         out.println("Time stamp: " + new Date().toString());
         out.println("<BR>Created bean (row) with the id of: " +
request.getParameter("ID"));
         out.println("</BODY>");
         out.println("</HTML>");
      }
      catch (Throwable exception) {
         throw new ServletException(
            "Unable to create TestEnt instance: " +
exception.toString());
      }






   }

   // Displays a trace message to System.out
   private void trace(String methodName) {
      System.out.print(methodName);
      System.out.println("() called");
   }

}



2) Modify your \orion-primer\src\xml\ejb\ejb-jar.xml to look like the
following:

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
          <enterprise-beans>
        <entity>
             <ejb-name>hello.ejb.TestEnt</ejb-name>
             <home>hello.ejb.TestEntHome</home>
             <remote>hello.ejb.TestEnt</remote>
             <ejb-class>hello.ejb.TestEntBean</ejb-class>
             <persistence-type>Container</persistence-type>
             <prim-key-class>hello.ejb.TestEntPK</prim-key-class>
             <reentrant>False</reentrant>
             <cmp-field>
               <field-name>id</field-name>
             </cmp-field>
          </entity>
          <session>
            <description></description>
            <display-name>hello.ejb.Hello</display-name>
            <ejb-name>hello.ejb.Hello</ejb-name>
            <home>hello.ejb.HelloHome</home>
            <remote>hello.ejb.Hello</remote>
            <ejb-class>hello.ejb.HelloBean</ejb-class>
            <session-type>Stateless</session-type>
         </session>
     </enterprise-beans>
      <assembly-descriptor>
         <container-transaction>
           <method>
             <ejb-name>TestEntBean</ejb-name>
             <method-name>*</method-name>
           </method>
           <trans-attribute>Required</trans-attribute>
         </container-transaction>
         <security-role>
            <description>Users</description>
            <role-name>users</role-name>
         </security-role>
         <method-permission>
            <description>Restricted</description>
            <role-name>users</role-name>
            <method>
               <ejb-name>hello.Hello</ejb-name>
               <method-name>*</method-name>
            </method>
         </method-permission>
      </assembly-descriptor>

</ejb-jar>


3) Modify your \orion-primer\src\xml\web\web.xml to look like the
following:

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
   <display-name>Orion Primer Web Application</display-name>
   <servlet>
      <servlet-name>hello.web.HelloServlet</servlet-name>
      <description>Servlet that calls the Hello bean</description>
      <servlet-class>hello.web.HelloServlet</servlet-class>
   </servlet>
   <servlet>
      <servlet-name>hello.web.TestEntServlet</servlet-name>
      <description>Servlet that calls the Test bean</description>
      <servlet-class>hello.web.TestEntServlet</servlet-class>
   </servlet>


   <ejb-ref>
      <ejb-ref-name>ejb/HelloHome</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <home>hello.ejb.HelloHome</home>
      <remote>hello.ejb.Hello</remote>
   </ejb-ref>
   <ejb-ref>
      <ejb-ref-name>ejb/TestEntHome</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <home>hello.ejb.TestEntHome</home>
      <remote>hello.ejb.TestEnt</remote>
    </ejb-ref>


   <servlet-mapping>
      <servlet-name>hello.web.HelloServlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
      <servlet-name>hello.web.TestEntServlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

4) Run ant from the orion-primer directory.
5) Start up orion server from the \orion\ directory (java �jar
orion.jar).  This should deploy the application and create a directory
directory/file
\orion\application-deployments\orion-primer\orion-primer-ejb.jar\orion-ejb-jar.xml.
This is the file you modify to map the bean to the database.  It should
look like:

<?xml version="1.0"?>
<!DOCTYPE orion-ejb-jar PUBLIC "-//Evermind//DTD Enterprise JavaBeans
1.1 runtime//EN" "http://www.orionserver.com/dtds/orion-ejb-jar.dtd">

<orion-ejb-jar deployment-version="1.0.3" deployment-time="e1298911c3">
 <enterprise-beans>
  <entity-deployment name="hello.ejb.TestEnt"
location="hello.ejb.TestEnt" wrapper="EntityHomeWrapper1"
table="TestEnt" data-source="jdbc/DefaultEJBDS">
   <primkey-mapping>
    <cmp-field-mapping>
     <fields>
      <cmp-field-mapping name="id" persistence-name="id" />
     </fields>
    </cmp-field-mapping>
   </primkey-mapping>
  </entity-deployment>
  <session-deployment name="hello.ejb.Hello" location="hello.ejb.Hello"
wrapper="SessionHomeWrapper3" timeout="1200"
persistence-filename="hello.ejb.Hello" />
 </enterprise-beans>
 <assembly-descriptor>
  <security-role-mapping name="users">
  </security-role-mapping>
  <default-method-access>
   <security-role-mapping impliesAll="true" />
  </default-method-access>
 </assembly-descriptor>
</orion-ejb-jar>

6) In your browser type: http://localhost/servlet/TestEntServlet?ID=1.
This should create a row in the TestEnt table (specified in the xml file
above) with an id of 1.



Reply via email to