KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > basic > CourseBean


1 package example.cmp.basic;
2
3 import javax.ejb.*;
4 import javax.naming.*;
5
6 import javax.sql.DataSource;
7 import java.io.Serializable;
8 import java.util.Enumeration;
9 import com.caucho.ejb.AbstractEntityBean;
10
11 /**
12  * Implementation class for the Course bean.
13  *
14  * <p>The implementation class of the <code>Course</code> entity bean.
15  * Its methods will be called only by the EJB container, never directly
16  * by clients. Clients always call methods in the local interface, calling
17  * methods in the Resin-CMP generated stub. The stub methods will call
18  * the CourseBean methods in the correct transaction context.
19  *
20  * <p><code>AbstractEntityBean</code> is a convenience superclass
21  * that provides a set of methods required by the spec that we don't use in
22  * this example.
23  *
24  * <p>This CMP entity bean use the following schema:
25  *
26  * <code><pre>
27  * CREATE TABLE basic_courses (
28  * id VARCHAR(250) NOT NULL,
29  *
30  * instructor VARCHAR(250),
31  * PRIMARY KEY(id)
32  * );
33  * </pre></code>
34  *
35  * <p>The column names are generated from the field names.
36  * getCourseId becomes course_id and getInstructor becomes instructor.
37  * The table name is specified in the ejb deployment descriptor,
38  * cmp-basic.ejb.
39  */

40 public abstract class CourseBean extends AbstractEntityBean {
41   /**
42    * Returns the ID of the course, the primary key.
43    *
44    * <p>All container-managed fields are abstract since Resin-CMP will
45    * generate the SQL and JDBC calls to manage them.
46    *
47    * <p>Each cmp-field described in the deployment descriptor needs to
48    * be matched in the implementation class by abstract setXXX and
49    * getXXX methods. The container will take care of implementing them.
50    *
51    * <p>Note that unless you make these methods available in the
52    * local interface, you will never be able to access them
53    * from an EJB client such as a servlet.
54    */

55   public abstract String getId();
56
57   /**
58    * Sets the primary key of the course, only called from a create method.
59    * The primary key may only be set in an ejbCreate method. Tables
60    * that generate the primary key automatically will not define the
61    * setXXX method, only the getXXX method.
62    *
63    * @param courseId the courseId of the new course
64    */

65   public abstract void setId(String courseId);
66
67   /**
68    * Returns the name of the instructor for this course. Since
69    * getInstructor is a container managed field (cmp-field),
70    * it must be abstract for Resin-CMP to implement.
71    *
72    * <p>Resin-CMP will automatically cache the value so most
73    * calls to getInstructor can avoid database calls.
74    */

75   public abstract String getInstructor();
76
77   /**
78    * Sets the name of the instructor for this course. Since setInstructor
79    * is a cmp-field, Resin-CMP will implement it. The value will be
80    * written to the database when the transaction completes.
81    */

82   public abstract void setInstructor(String instructor);
83
84   /**
85    * Swaps the instructors. Resin-CMP encapsulates all business methods
86    * a transaction to protect against concurrent access. So client code
87    * can call the business method without worrying about database
88    * consistency.
89    *
90    * <p>Because concurrent modifications can conflict, it's always possible
91    * for a business method to throw a transaction failure exception, a
92    * runtime exception. Depending on the business logic, clients can retry
93    * the transaction or just return an error message.
94    *
95    * @param course the course with the instructor to swap.
96    *
97    * @exception EJBException if the transaction fails.
98    */

99   public void swap(Course course)
100   {
101     String temp = getInstructor();
102     setInstructor(course.getInstructor());
103     course.setInstructor(temp);
104   }
105 }
106
107     
108
Popular Tags