Relationships


Using Multiple Entity Beans

In order to show some advanced topics using beans, let's create another bean and database table. The new table is called groups. The groups table will be linked to the login table using a new field you'll add to login. First, though, the schema for ent_:

 create table groups (      group_Id Int not null primary key auto_Increment,      groups varchar(3),      ts timestamp ); 

Now, populate a couple rows so you have some information to view:

 insert into groups values(0, 'aab', now()); insert into groups values(0, 'bbc', now()); 

The result of these insertions is a table, as shown here:

 mysql> select * from groups; +----------+--------+----------------+ | group_id | groups | ts             | +----------+--------+----------------+ |        1 | aab    | 20030109092527 | |        2 | bbc    | 20030109092534 | +----------+--------+----------------+ 2 rows in set (0.00 sec) 

You need to relate the rows in login to the rows in the groups table. This is accomplished in two steps:

  1. Add a new column to login.

  2. Update the rows in login.

First, add a new column to login with this SQL:

 alter table login add column currGroup int; 

This code adds a new column to each row in the login table. Now update the rows:

 Update login set currGroup = 1 where login='johnd'; Update login set currGroup = 2 where login='janed'; 

With the new update statements, you have successfully linked the rows in the login table with the rows in the groups table, as shown in Figure 6.11.

click to expand
Figure 6.11: The login and groups table relationship.

Adding a Groups Bean

To cut down on the code needed for the group bean, let's write a single finder method that will locate all rows in the groups table. Using everything you've learned so far, define the bean called Group for the groups table using the three files shown in Listings 6.2, 6.3, and 6.4.

Listing 6.2: groupHome.java.

start example
 package entitlements; import javax.ejb.*; import java.util.*; public interface GroupHome extends EJBLocalHome {   Group findByPrimaryKey(int id)    throws FinderException;   Collection findByLogin(String login)    throws FinderException; } 
end example

Listing 6.3: groupHome.java.

start example
 package entitlements; import javax.ejb.*; import java.sql.Timestamp; import java.util.*; public interface Group extends EJBLocalObject {   int       getGroup_id();   String    getGroups();   String    getTS();   void      setGroups(String g);   void      setTS(String t); } 
end example

Listing 6.4: groupBean.java.

start example
 package entitlements; import javax.ejb.*; import java.sql.Timestamp; import java.util.*; public abstract class GroupBean   extends com.caucho.ejb.AbstractEntityBean {   public abstract int       getGroup_id();   public abstract String    getGroups();   public abstract String    getTS();   public abstract void      setGroups(String g);   public abstract void      setTS(String t); } 
end example

The entry in the deployment descriptor for the bean is:

 <entity>   <ejb-class>entitlements.GroupBean</ejb-class>   <persistence-type>Container</persistence-type>   <prim-key-class>int</prim-key-class>   <reentrant>True</reentrant>   <abstract-schema-name>groups</abstract-schema-name>   <sql-table>groups</sql-table>   <cmp-field><field-name>groups</field-name></cmp-field>   <cmp-field><field-name>ts</field-name></cmp-field>   <primkey-field>group_id</primkey-field>   <query>     <query-method>       <method-name>findAll</method-name>     </query-method>     <ejb-ql>SELECT o FROM groups o</ejb-ql>   </query>   <local-home>entitlements.GroupHome</local-home>   <local>entitlements.Group</local>   <ejb-name>Group</ejb-name> </entity> 




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net