|
| |
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
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:
- Add the createXXX method to the local home interface.
- Implement a corresponding ejbCreateXXX method in the bean class, setting
any database fields.
- Implement a corresponding ejbPostCreateXXX method in the bean
class, setting any bean relations if necessary.
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)
);
|
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();
...
|
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;
}
|
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
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|