resin-ee $ee/ejb-tut/cmp-create.xtp Creating and Removing Entity Beans

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
tutorial create

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 method to the local home interface.
  2. Implement a corresponding method in the bean class, setting any database fields.
  3. Implement a corresponding method in the bean class, setting any bean relations if necessary.

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

CREATE TABLE create_courses ( course_id VARCHAR(250) NOT NULL, instructor VARCHAR(250), PRIMARY KEY(course_id) );

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 interface. All we need to do is call it.

... 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); out.println(course.getCourseId() + " is taught by " + course.getInstructor() + "<br>"); } divination.remove(); creatures.remove(); ...

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

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; }

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

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

The 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.

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. } }