Package example.cmp.single_table

The simplest example uses a CMP bean to manage a single database table.

See:
          Description

Interface Summary
House The client's view of a row in the house table.
HouseHome The client's view of the house table.
 

Class Summary
HouseBean Implementation of the HouseBean.
HouseServlet Example database client servlet using the house table.
 

Package example.cmp.single_table Description

The simplest example uses a CMP bean to manage a single database table. The developer creates the database schema and designs the bean interface. Resin-CMP manages and caches access to the database table. The bean is intended to be used in the same web-app as its servlets and JSP pages.

The example has a table of boarding school dorms. Each house has a name and some points used to determine the winning house at the end of the year.

The developer designs three classes: the bean implementation, the home interface, and the local interface. HouseBean, the bean implementation, defines abstract accessor methods for the fields to be managed by Resin-CMP and any additional business method implementation. HouseHome, the home interface, defines a findByPrimaryKey method to lookup houses. House, the local interface, defines the methods callable by client servlets and JSPs in the web-app.

SQL Schema

CREATE TABLE house (
  name VARCHAR(250) NOT NULL,
  points INTEGER,

  PRIMARY KEY(name)
)

Server Configuration

The CMP server is configured as a servlet in the web.xml. It will load all the *.ejb files in WEB-INF and instantiate the servers.
<servlet-mapping>
  <url-pattern id='/example/*'/>
  <servlet-name id='com.caucho.burlap.EJBServlet'/>
  <init-param url-prefix="http://localhost:8080/cmp/example"/>
  <init-param jdbc-ref='java:comp/env/jdbc/test'/>
  <load-on-startup/>
</servlet-mapping>

JNDI client configuration

The HouseServlet client will look up the HouseHome using JNDI. JNDI is configured in the web.xml to point to the local CMP server.
<jndi-link>
  <jndi-name>java:comp/env/cmp</jndi-name>
  <jndi-factory>com.caucho.ejb.LocalContextFactory</jndi-factory>
  <init-param java.naming.provider.url="http://localhost:8080/cmp/example"/>
</jndi-link>

Deployment Descriptor

With Resin-CMP, the deployment descriptor will normally be a single_table.ejb file in WEB-INF. EJBServlet will automatically load all *.ejb files in WEB-INF, automatically deploying the CMP beans.
<!--
   - The EJB deployment descriptor for the single_table example.
   -
   - The example has a single database table showing the most basic
   - bean backed by a database.  It's strictly a local example, indended
   - to be used as the database store within a single web-app.
   -
   - Because it has no remote interface, it can only be accessed
   - from the same web-app.
  -->
<ejb-jar>
<enterprise-beans>
  <!--
     - Each entity bean corresponds to a database table.  In the
     - deployment descriptor, each bean has its own <entity> block.
    -->
  <entity>
    <!--
       - ejb-name is used to look up the bean's home interface.
       -
       - Normally, it will be used in a JNDI name like
       -  java:comp/env/cmp/single_table
      -->
    <ejb-name>single_table</ejb-name>

    <!--
       - ejb-class is the implementation class.
      -->
    <ejb-class>example.cmp.single_table.HouseBean</ejb-class>

    <!--
       - local-home is used to find and create instances.  It's only
       - available in the same web-app (i.e. same classloader.)
      -->
    <local-home>example.cmp.single_table.HouseHome</local-home>

    <!--
       - local is the interface for a single instance. It's only
       - available in the same web-app (i.e. same classloader.)
      -->
    <local>example.cmp.single_table.House</local>

    <!--
       - persistence-type defines that Resin-CMP will handle the database
       - access.
      -->
    <persistence-type>Container</persistence-type>

    <!--
       - reentrant true means that the bean can call itself.
      -->
    <reentrant>True</reentrant>

    <!--
       - abstract-schema-name specifies the abstract database table name.
       - If no other mapping is defined, it will be used for the actual
       - database table name.
      -->
    <abstract-schema-name>houses</abstract-schema-name>

    <!--
       - prim-key-class defines the Java class of the primary key.
      -->
    <prim-key-class>String</prim-key-class>

    <!--
       - primkey-field specifies the field to be used for the primary key.
      -->
    <primkey-field>name</primkey-field>

    <!--
       - cmp-field defines the fields to be managed by Resin-CMP.
      -->
    <cmp-field><field-name>name</field-name></cmp-field>
    <cmp-field><field-name>points</field-name></cmp-field>
  </entity>
</enterprise-beans>
</ejb-jar>