这个系列文章我是根据 Rick Hightower 发表在 IBM 网站的文章
《
EJB - CMP/CMR 介绍》 改编而成的,这是一个系列,共有4篇文章。这篇文章综合了那4篇文章,看这篇文章前一定要参考一下,这样理解起来很方便,不过我做了部分修改,原理基本还是不变的。关于 CMP 方面的知识我不说了,我这里只是介绍如何使用 Lomboz 和 JBoss-IDE 在Eclipse 中开发 CMP。
/**
* Home method
* @throws FinderException
* @ejb.home-method view-type = "local"
*/
public java.util.ArrayList ejbHomeGetUserIDs(String groupName)
throws javax.ejb.FinderException {
return (java.util.ArrayList) ejbSelectUserIDs(groupName);
}
这篇文章主要讲述如何开发一对多的双向联系,这里设计了一个GroupBean,和UserInfoBean建立联系(注意,IBM 网站的文章是和UserBean建立联系,这里我认为和UserInfoBean建立联系更合适)。重点放在如何使用XDoclet建立一对多的双向联系,客户端的调用比较简单。
这篇文章是由 《
Eclipse快速上手EJB -- 2. 设计一个实体Bean》、《
Eclipse快速上手EJB -- 3. 设计一对一的双向关系的CMR(1)》 《
Eclipse快速上手EJB -- 4. 多对多的单向关系的CMR(1) 》延伸而来。所以,必须要有这三篇文章的基础才行。
关于环境配置、使用技巧还是要参考我以前的文章,这里尽量省略。
一、设计实体Bean :GroupBean
1. 建立主体部分:
●
在 CMP_Sample 项目中,右击“src” ->新建 ->Lomboz EJB Creation Wizard :
· 包(K) :javamxj.ejb.cmp
· 名称(M): Group
· EJB Type: 选择 Container Managed Entity EJB
点击下一步。
● Schema Name: GroupSchema
Table Name: GroupTable
● 增加一个 name 栏:
并且使它成为主键。
· Field: name
· Field Type: java.lang.String
· Database Column: 名称
· SQL Type: varchar
● 同样,再增加一个 description 栏:
· Field: description
· Field Type: java.lang.String
· Database Column: 行业说明
· SQL Type: varchar
效果如下,最后点击完成。
注意:如上篇文章一样,将生成代码中的 sql-type="varchar" 改写成 sql-type="varchar(xy)" ,xy是一个合适的VARCHAR 的位数。
2.
完成 ejbCreate 和 ejbPostCreate 方法:
3. 在类标记中加入以下标记:
4. 增加一个Select method ,你可以使用JBoss-IDE得到,这里是代码(后面有完整的源代码):
//######################## Select method ##############################
/**
* Select method
* @ejb.select
* query = "SELECT user.email FROM GroupSchema AS g, IN (g.users) AS user WHERE g.name = ?1"
*
*/
public abstract java.util.Collection ejbSelectUserIDs(String groupName)
throws javax.ejb.FinderException;
/**
* Select method
* @ejb.select
* query = "SELECT user.email FROM GroupSchema AS g, IN (g.users) AS user WHERE g.name = ?1"
*
*/
public abstract java.util.Collection ejbSelectUserIDs(String groupName)
throws javax.ejb.FinderException;
/**
* Home method
* @throws FinderException
* @ejb.home-method view-type = "local"
*/
public java.util.ArrayList ejbHomeGetUserIDs(String groupName)
throws javax.ejb.FinderException {
return (java.util.ArrayList) ejbSelectUserIDs(groupName);
}
5. 将 GroupBean 加入到 cmpEJB 模块中,然后 lomboz ->Generate EJB Classes
6. 重点:建立 GroupBean 与 UserInfoBean 的一对多的双向联系:
● 右击 GroupBean ->J2EE ->Add CMR Relationship :
生成的代码参考下面给出的完整的源代码。
● 切换到
UserInfoBean.java:
右击 UserInfoBean ->J2EE ->Add CMR Relationship :
在 UserInfoBean.java 中生成的代码(需要手工添加
@jboss.relation):
/**
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "GroupsHaveUserInfos"
* role-name = "UserInfosInGroup"
* target-multiple = "yes"
* @jboss.relation
* fk-column = "组别"
* related-pk-field = "name"
*/
public abstract javamxj.ejb.cmp.GroupLocal getGroup();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public abstract void setGroup(javamxj.ejb.cmp.GroupLocal value);
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "GroupsHaveUserInfos"
* role-name = "UserInfosInGroup"
* target-multiple = "yes"
* @jboss.relation
* fk-column = "组别"
* related-pk-field = "name"
*/
public abstract javamxj.ejb.cmp.GroupLocal getGroup();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public abstract void setGroup(javamxj.ejb.cmp.GroupLocal value);
Ok,再次 Generate EJB Classes ,如果一切正常,再进行下一步。
● 下面给出了GroupBean.java的完整的源代码:
GroupBean.java
/* * 创建日期 2005-1-26 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.cmp; /** * * <!-- begin-user-doc --> You can insert your documentation for '<em><b>GroupBean</b></em>'. <!-- end-user-doc --> * <!-- begin-lomboz-definition --> <?xml version="1.0" encoding="UTF-8"?> <lomboz:EJB xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:lomboz="http://lomboz.objectlearn.com/xml/lomboz"> <lomboz:entity> <lomboz:entityEjb> <j2ee:display-name>Group</j2ee:display-name> <j2ee:ejb-name>Group</j2ee:ejb-name> <j2ee:ejb-class>javamxj.ejb.cmp.GroupBean</j2ee:ejb-class> <j2ee:persistence-type>Container</j2ee:persistence-type> <j2ee:prim-key-class>java.lang.String</j2ee:prim-key-class> <j2ee:cmp-version>2.x</j2ee:cmp-version> <j2ee:abstract-schema-name>GroupSchema</j2ee:abstract-schema-name> <j2ee:primkey-field>name</j2ee:primkey-field> </lomboz:entityEjb> <lomboz:fieldMappings> <lomboz:fieldName>name</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>名称</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>true</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:fieldMappings> <lomboz:fieldName>description</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>行业说明</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>false</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:tableName>GroupTable</lomboz:tableName> <lomboz:dataSourceName></lomboz:dataSourceName> </lomboz:entity> </lomboz:EJB> <!-- end-lomboz-definition --> * * <!-- begin-xdoclet-definition --> * @ejb.bean name="Group" * jndi-name="Group" * type="CMP" * primkey-field="name" * schema="GroupSchema" * cmp-version="2.x" * view-type = "local" * data-source="" * * @ejb.persistence * table-name="GroupTable" * * * * @ejb.finder * query="SELECT OBJECT(a) FROM GroupSchema as a" * signature="java.util.Collection findAll()" * * @ejb.pk class="java.lang.String" * <!-- end-xdoclet-definition --> * @generated **/ public abstract class GroupBean implements javax.ejb.EntityBean {
/** * @ejb.create-method */ public java.lang.String ejbCreate(String name, String description)
throws javax.ejb.CreateException {
// EJB 2.0 spec says return null for CMP ejbCreate methods. setName(name); setDescription(description); return null;
}
/** * The container invokes this method immediately after it calls ejbCreate. */ public void ejbPostCreate(String name, String description)
throws javax.ejb.CreateException {
}
/** * CMP Field name * @return the name * @ejb.persistent-field * @ejb.persistence * column-name="名称" * jdbc-type="VARCHAR" * sql-type="varchar(24)" * read-only="false" * @ejb.pk-field * * @ejb.interface-method */ public abstract java.lang.String getName();
/** * @param java.lang.String the new name value * @ejb.interface-method */ public abstract void setName(java.lang.String name);
/** * CMP Field description * @return the description * @ejb.persistent-field * @ejb.persistence * column-name="行业说明" * jdbc-type="VARCHAR" * sql-type="varchar(24)" * read-only="false" * * * @ejb.interface-method */ public abstract java.lang.String getDescription();
/** * @param java.lang.String the new description value * @ejb.interface-method */ public abstract void setDescription(java.lang.String description);
//######################## Select method ############################## /** * Select method * @ejb.select * query = "SELECT user.email FROM GroupSchema AS g, IN (g.users) AS user WHERE g.name = ?1" * */ public abstract java.util.Collection ejbSelectUserIDs(String groupName)
throws javax.ejb.FinderException;
/** * Home method * @throws FinderException * @ejb.home-method view-type = "local" */ public java.util.ArrayList ejbHomeGetUserIDs(String groupName)
throws javax.ejb.FinderException {
return (java.util.ArrayList) ejbSelectUserIDs(groupName);
}
// ######################## CMR Relationship ############################## /** * Getter for CMR Relationship * * @ejb.interface-method view-type="local" * @ejb.relation name = "GroupsHaveUserInfos" * role-name = "GroupHasUserInfos" * target-multiple = "no" */ public abstract java.util.Collection getUsers();
/** * Setter for CMR Relationship * * @ejb.interface-method view-type="local" */ public abstract void setUsers(java.util.Collection value);
}