|
|  |
Find this tutorial in: /opt/app-servers/resin-3.0.18/webapps/resin-doc/amber/tutorial/cmp-basic
Try the Tutorial
Basic CMP example showing configuration, classes, and client code for a single-table bean.
This example focuses on:
- Introduces Container Managed Persistance (CMP) fundamental concepts
- Setting up the database to work with CMP
- Developing the EntityBean classes
- Developing a Servlet to lookup and use the entity bean
- Configuring Resin to deploy the EJB and use JNDI
CMP manages tables in a relational database using a Java
bean interface. Each database table corresponds to a single "entity bean".
By creating an entity bean with container managed
persistence, you let CMP generate the SQL to load, store, and
cache entity beans from the database. Avoiding SQL is an advantage in
itself, but the primary advantage is the increased flexiblity of your
application code. Maintenance and code-refactoring can focus on the
beans instead of changing lots of SQL statements in the program.
course.sql
CREATE TABLE basic_courses (
id INTEGER PRIMARY KEY auto_increment,
course VARCHAR(250),
teacher VARCHAR(250)
);
INSERT INTO basic_courses VALUES('Potions', 'Severus Snape');
INSERT INTO basic_courses VALUES('Transfiguration', 'Minerva McGonagall');
|
CourseBean.java
package example;
@javax.ejb.Entity
@javax.ejb.Table (name="ejb3_basic_course")
public class CourseBean {
private int _id;
private String _course;
private String _teacher;
@javax.ejb.Id (generator=javax.ejb.GeneratorType.AUTO)
@javax.ejb.Column (name="id")
public int getId()
{
return _id;
}
public void setId(int id)
{
_id = id;
}
@javax.ejb.Basic
public String getCourse()
{
return _course;
}
public void setCourse(String course)
{
_course = course;
}
@javax.ejb.Basic
public String getTeacher()
{
return _teacher;
}
public void setTeacher(String teacher)
{
_teacher = teacher;
}
}
|
With Resin, all the Java source can be dropped in WEB-INF/classes.
Resin will automatically compile any changes and regenerate the persistence
classes, stubs and skeletons.
Now that we've built the bean, we need to
attach it to Resin. The entity bean is deployed using
the ejb-server resource.
WEB-INF/web.xml
<web-app>
<!-- server configuration -->
<ejb-server data-source="jdbc/resin">
<bean type="example.CourseBean"/>
</ejb-server>
<servlet servlet-name="basic" servlet-class="example.CourseServlet">
<init entity-manager="com.caucho.amber.ejb3.EntityManagerProxy@bc412c"/>
</servlet>
<servlet-mapping url-pattern="/basic" servlet-name="basic"/>
</web-app>
|
CourseServlet.java
import javax.ejb.EntityManager;
public class CourseServlet extends HttpServlet {
private EntityManager _manager;
public void setEntityManager(EntityManager manager)
{
_manager = manager;
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws java.io.IOException, ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
CourseBean []course = new CourseBean[2];
course[0] = (CourseBean) _manager.find("CourseBean", new Integer(1));
course[1] = (CourseBean) _manager.find("CourseBean", new Integer(2));
out.println("<h3>Course Details</h3>");
for (int i = 0; i < course.length; i++) {
out.println("course: " + course[i].getCourse() + "<br>");
out.println("teacher: " + course[i].getTeacher() + "<br>");
out.println("<br>");
}
}
}
|
Course Details
course: Potions
instructor: Severus Snape
course: Transfiguration
instructor: Minerva McGonagall
|
The core of EJB's database management is its management of a
single table. Much of the work underlying the database management is
hidden from the applicaton. Transaction management and caching happen
automatically. For example, once the course has been loaded from the
database, Resin-CMP does not need to query the database again until
the course changes. So read-only requests, the most common, can avoid
all database traffic.
More complicated applications build on the single table
management. The following examples add more realistic features to
this example: using queries to find all
courses and creating new database rows.
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. |  |
|