Creating and Removing Entity Beans
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

User's Guide
Reference
Tutorials
Scrapbook

Basic CMP
Find with EJB-QL
Creating and Removing
1-n Relationship
ejbSelect EJB-QL
1-1 Relationship
n-m Relationship
Map/Compound PK
Find with EJB-QL
Tutorials
1-n Relationship

Find this tutorial in: /usr/local/resin/webapps/resin-doc/cmp/tutorial/cmp-create
Try the Tutorial

Scenario: Headmaster Dumbledore adds two courses to the curriculum, then changes his mind and removes them again

This example focuses on:

  • Implementing and using create, ejbCreate, and ejbPostCreate methods
  • Using the remove() method for entity beans

  1. Database Schema
  2. Client Servlet
  3. Adding Create to CourseHome
  4. ejbCreate and ejbPostCreate in CourseBean

Almost all applications need to add and remove entities from the database. Although most database accesses are reads, eventually we need to change the database. With Resin-EE, you add a create method with the following steps:

  1. Add the createXXX method to the local home interface.
  2. Implement a corresponding ejbCreateXXX method in the bean class, setting any database fields.
  3. Implement a corresponding ejbPostCreateXXX method in the bean class, setting any bean relations if necessary.

Database Schema

The example uses the same database table as the previous basic example.

create.sql
CREATE TABLE create_courses (
  course_id VARCHAR(250) NOT NULL,
  instructor VARCHAR(250),

  PRIMARY KEY(course_id)
);

Client Servlet

Clients simply call the create method from the CourseHome interface. The following servlet fragment creates two new courses, prints all the courses, and then removes the new courses.

The remove() method is generated automatically and is always part of the EJBLocalObject interface. All we need to do is call it.

Adding and Removing Courses
...

divination = home.create("Divination", "Sybil Trelawney");
creatures = home.create("Care of Magical Creatures", "Rubeus Hagrid");

Collection c = home.findAll();
Iterator iter = c.iterator();

while (iter.hasNext()) {
  Course course = (Course) iter.next();

  out.println(course.getCourseId() + " is taught by " +
              course.getInstructor() + "<br>");
}

divination.remove();
creatures.remove();
...

Adding Create to CourseHome

The CourseHome interface adds the create method. Most than one create method is allowed, but all must start with create. createWithInstructor or createChild would be allowed. Any create method must throw CreateException.

CourseHome.java
package example.cmp.create;

import java.util.*;
import javax.ejb.*;

public interface CourseHome extends EJBLocalHome {
  Course findByPrimaryKey(String name)
    throws FinderException;

  Course create(String courseId, String instructor)
    throws CreateException;
}

ejbCreate and ejbPostCreate in CourseBean

Creating a bean needs to methods in the bean class: ejbCreate and ejbPostCreate. ejbCreate saves cmp-fields before inserting into the database. ejbPostCreate links persistent relations. In this example, the ejbPostCreate method will be empty. In the example of user comments to an article, the ejbPostCreate method will link the new comment object to the owning article.

  • ejbCreate sets the primary key and cmp-fields
  • ejbPostCreate sets the cmr-fields (relations to other beans)

The ejbCreate method is the only method that may call the setter for the primary key. Database tables which generate their own primary key (e.g. autoincrement ids), will have no setter for the primary key.

CourseBean.java
package example.cmp.find;

abstract public class CourseBean
  extends com.caucho.ejb.AbstractEntityBean {
  abstract public String getCourseId();
  abstract public void setCourseId(String id);

  abstract public String getInstructor();
  abstract public void setInstructor(String instructor);

  public String ejbCreate(String course, String instructor)
    throws CreateException
  {
    setCourseId(course);
    setInstructor(instructor);

    return course;
  }

  public void ejbPostCreate(String course, String instructor)
  {
    // since there are no relations, this is empty.
  }
}

Try the Tutorial


Find with EJB-QL
Tutorials
1-n Relationship
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.