|
EJB 2 - Les Entreprise Java Bean (JavaBeans)
6.3.Développement des Entity
Bean
La première étape de notre développement sera la
création de nos différents entity beans.
Grâce à nos différents outils, nous avons juste à
créer les classes de nos beans, les interfaces et autres
fichiers liés seront générés
automatiquement (via xDoclet).
De ce fait, nous ne présenterons que les classes bean de nos
EJB (les interfaces sont accessibles dans l’annexe).
Nos Entity bean sont de type CMP, nous devrons donc suivre l’ensemble
des normes citées au début de ce document en ce qui
concerne ce type d’EJB.
6.3.1.Country
Voici le code de la classe du bean de l’EJB Country:
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
javax.ejb.EJBException;
import
javax.ejb.EntityBean;
import
javax.ejb.EntityContext;
import
javax.ejb.RemoveException;
/**
*
@ejb.bean name="Country"
*
display-name="Name for Country"
*
description="Description for Country"
*
local-jndi-name="ejb/Country"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
reentrant = "false"
*
primkey-field = "countryId"
*
schema = "Country"
*
*
@ejb.pk class = "java.lang.Integer"
*
*
@jboss.persistence create-table = "true" table-name =
"Country" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
*
*/
public
abstract class CountryBean implements EntityBean {
public
CountryBean() {
super();
}
public
void setEntityContext(EntityContext ctx)
throws
EJBException,
RemoteException
{
}
public
void unsetEntityContext() throws EJBException, RemoteException {
}
public
void ejbRemove()
throws
RemoveException,
EJBException,
RemoteException
{
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
public
void ejbLoad() throws EJBException, RemoteException {
}
public
void ejbStore() throws EJBException, RemoteException {
}
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
Integer ejbCreate(Integer countryId, String name) throws
javax.ejb.CreateException {
setCountryId(countryId);
setName(name);
return
null;
}
/**
* Post Create method
*/
public
void ejbPostCreate(Integer countryId, String name) throws
javax.ejb.CreateException {
}
/**
* Getter for CMP Field countryId
*
* @ejb.pk-field
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract java.lang.Integer getCountryId();
/**
* Setter for CMP Field countryId
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setCountryId(java.lang.Integer value);
/**
* Getter for CMP Field name
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getName();
/**
* Setter for CMP Field name
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setName(String value);
}
Voici une description des différents tags utilisés :
-
ejb.bean : permet d’indiquer à xDoclet que notre
classe correspond à un EJB
*
@ejb.bean name="Country"
*
display-name="Name for Country"
*
description="Description for Country"
*
local-jndi-name="ejb/Country"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
reentrant = "false"
*
primkey-field = "countryId"
*
schema = "Country"
-
name : nom de l’EJB au niveau du système
-
display-name : nom de l’EJB à afficher dans la
console d’administration
-
description : description de l’EJB
-
local-jndi-name : nom JNDI attribué pour accéder
à l’EJB en local
-
type : type de l’EJB (ici Container Managed Persistance)
-
cmp-version : version à utiliser pour le descripteur de
déploiement pour le CMP
-
view-type : vue du client par rapport à l’EJB
(ici local seulement)
-
reentrant : l’EJB permet la réentrance ou non
(cf. services avancés)
-
primkey-field : clé primaire à utiliser
-
schema : nom à utiliser dans les requêtes EJB-QL
pour cet EJB
-
ejb.pk : paramétrage de la clé primaire
*
@ejb.pk class = "java.lang.Integer"
-
class : classe définissant le type de la clé
primaire (ici Integer)
-
jboss.persistence : paramétrage lié à
JBoss pour la gestion de la persistance au niveau du serveur
*
@jboss.persistence create-table = "true" table-name =
"Country" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
-
create-table : création des tables lors du déploiement
de l’EJB
-
table-name : nom de la table à utiliser pour cet EJB
-
datasource : datasource à utiliser pour cet EJB
-
datasource-mapping : type de mapping à utiliser pour
cette datasource
-
remove-table : suppression des tables lors du « undeployed »
de l’EJB
-
ejb.create-method : déclare une méthode create
pour cet EJB
*
@ejb.create-method view-type = "local"
-
view-type : définit le type d’accès à
cette méthode (ici local)
-
ejb.pk-field : déclare un champ de type clé
primaire
* @ejb.pk-field
-
ejb.persistent-field : déclare ce champ comme persistent
* @ejb.persistent-field
-
ejb.interface-method : déclare une méthode métier
* @ejb.interface-method view-type="local"
-
view-type : type d’accès pour cette méthode
(ici local)
6.3.2.City
Voici le code du bean City :
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
javax.ejb.EJBException;
import
javax.ejb.EntityBean;
import
javax.ejb.EntityContext;
import
javax.ejb.RemoveException;
import
com.society.stockmanager.interfaces.CountryLocal;
/**
*
@ejb.bean name="City"
*
display-name="Name for City"
*
description="Description for City"
*
local-jndi-name="ejb/City"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
primkey-field = "cityId"
*
reentrant = "false"
*
schema = "City"
*
*
@ejb.pk class = "java.lang.Integer"
*
*
@jboss.persistence create-table = "true" table-name =
"City" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
*
*/
public
abstract class CityBean implements EntityBean {
public
CityBean() {
super();
}
public
void setEntityContext(EntityContext ctx)
throws
EJBException,
RemoteException
{
}
public
void unsetEntityContext() throws EJBException, RemoteException {
}
public
void ejbRemove()
throws
RemoveException,
EJBException,
RemoteException
{
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
public
void ejbLoad() throws EJBException, RemoteException {
}
public
void ejbStore() throws EJBException, RemoteException {
}
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
Integer ejbCreate(Integer cityId, String name, CountryLocal country)
throws javax.ejb.CreateException {
setCityId(cityId);
setName(name);
return
null;
}
/**
* Post Create method
*/
public
void ejbPostCreate(Integer cityId, String name, CountryLocal country)
throws javax.ejb.CreateException {
setCountry(country);
}
/**
* Getter for CMP Field cityId
*
* @ejb.pk-field
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract Integer getCityId();
/**
* Setter for CMP Field cityId
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setCityId(Integer value);
/**
* Getter for CMP Field name
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getName();
/**
* Setter for CMP Field name
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setName(String value);
/**
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "country"
* target-ejb = "Country"
* role-name = "city-country"
* target-role-name = "country-city"
* target-multiple = "yes"
*
* @jboss.relation related-pk-field="countryId"
fk-column="countryId" fk-constraint="true"
*/
public
abstract com.society.stockmanager.interfaces.CountryLocal
getCountry();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setCountry(
com.society.stockmanager.interfaces.CountryLocal
value);
}
-
ejb.relation : définit une relation inter EJB
* @ejb.relation name = "country"
* target-ejb = "Country"
* role-name = "city-country"
* target-role-name = "country-city"
* target-multiple = "yes"
-
name : nom du champ persistant à lier
-
target-ejb : nom de l’EJB cible (ici Country)
-
role-name : nom du rôle à donner pour cette
relation (au niveau de l’EJB en cours : City)
-
target-role-name : nom du rôle à donner pour
cette relation (au niveau de l’EJB cible : Country)
-
target-multiple :
-
-
jboss.relation : paramètre la relation au niveau du
serveur JBoss
* @jboss.relation related-pk-field="countryId"
fk-column="countryId" fk-constraint="true"
-
related-pk-field : nom du champs primaire à utiliser
dans la relation (dans Country)
-
fk-column : nom du champ à créer dans l’EJB
courant (City)
-
fk-constraint : drapeau indiquant si une contrainte de clé
primaire doit être placée sur la colonne de la
relation
6.3.3.Provider
Voici le code du bean :
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
java.util.Collection;
import
javax.ejb.EJBException;
import
javax.ejb.EntityBean;
import
javax.ejb.EntityContext;
import
javax.ejb.RemoveException;
import
com.society.stockmanager.interfaces.CityLocal;
import
com.society.stockmanager.vo.ProviderData;
/**
*
@ejb.bean name="Provider"
*
display-name="Name for Provider"
*
description="Description for Provider"
*
local-jndi-name="ejb/Provider"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
reentrant = "false"
*
primkey-field = "providerId"
*
schema = "Provider"
*
*
@ejb.pk class = "java.lang.Integer"
*
*
@ejb.finder query = "select Object(p) from Provider p"
*
result-type-mapping = "Local"
*
signature = "Collection findAll()"
*
*
@jboss.persistence create-table = "true" table-name =
"Provider" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
*
*/
public
abstract class ProviderBean implements EntityBean {
public
ProviderBean() {
super();
}
public
void setEntityContext(EntityContext ctx)
throws
EJBException,
RemoteException
{
}
public
void unsetEntityContext() throws EJBException, RemoteException {
}
public
void ejbRemove()
throws
RemoveException,
EJBException,
RemoteException
{
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
public
void ejbLoad() throws EJBException, RemoteException {
}
public
void ejbStore() throws EJBException, RemoteException {
}
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
Integer ejbCreate(ProviderData datas,
CityLocal
city,
Collection
products) throws javax.ejb.CreateException {
setProviderId(datas.getProviderId());
setName(datas.getName());
setSociety(datas.getSociety());
setZipCode(datas.getZipCode());
return
null;
}
/**
* Post Create method
*/
public
void ejbPostCreate(ProviderData datas,
CityLocal
city,
Collection
products) throws javax.ejb.CreateException {
setProducts(products);
setCity(city);
}
/**
* Getter for CMP Field id
*
* @ejb.pk-field
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract Integer getProviderId();
/**
* Setter for CMP Field id
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setProviderId(Integer value);
/**
* Getter for CMP Field name
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getName();
/**
* Setter for CMP Field name
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setName(String value);
/**
* Getter for CMP Field society
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getSociety();
/**
* Setter for CMP Field society
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setSociety(String value);
/**
* Getter for CMP Field zipCode
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getZipCode();
/**
* Setter for CMP Field zipCode
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setZipCode(String value);
/**
* @ejb.interface-method view-type="local"
*
* @ejb.relation
* name="products-providers"
* role-name="providers-products"
*
* @jboss.relation
* related-pk-field="id"
* fk-column="productId"
* fk-constraint="true"
*/
public
abstract Collection getProducts();
/**
* @ejb.interface-method view-type="local"
*/
public
abstract void setProducts(Collection products);
/**
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "city"
* role-name = "provider-city"
* target-ejb = "City"
* target-role-name = "city-provider"
* target-multiple = "yes"
* @jboss.relation fk-column = "cityId" related-pk-field =
"cityId" fk-constraint = "true"
*/
public
abstract com.society.stockmanager.interfaces.CityLocal getCity();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setCity(
com.society.stockmanager.interfaces.CityLocal
value);
}
-
ejb.finder : définition d’un « finder »
pour cet EJB
*
@ejb.finder query = "select Object(p) from Provider p"
*
result-type-mapping = "Local"
*
signature = "Collection findAll()"
-
query : requête EJB-QL associé au finder
-
result-type-mapping : type de mapping retourné (local /
remote)
-
signature : prototype de la méthode dans l’interface
home
6.3.4.ProductFamily
Voici le code du bean :
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
javax.ejb.EJBException;
import
javax.ejb.EntityBean;
import
javax.ejb.EntityContext;
import
javax.ejb.RemoveException;
import
com.society.stockmanager.vo.ProductFamilyData;
/**
*
@ejb.bean name="ProductFamily"
*
schema = "ProductFamily"
*
display-name="Name for ProductFamily"
*
description="Description for ProductFamily"
*
local-jndi-name="ejb/ProductFamily"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
primkey-field = "code"
*
*
@ejb.pk class = "java.lang.String"
*
*
@ejb.finder query = "select Object(p) from ProductFamily p"
*
result-type-mapping = "Local"
*
signature = "Collection findAll()"
*
*
@jboss.persistence create-table = "true" table-name =
"ProductFamily" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
*
*/
public
abstract class ProductFamilyBean implements EntityBean {
public
ProductFamilyBean() {
super();
}
public
void setEntityContext(EntityContext ctx)
throws
EJBException,
RemoteException
{
}
public
void unsetEntityContext() throws EJBException, RemoteException {
}
public
void ejbRemove()
throws
RemoveException,
EJBException,
RemoteException
{
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
public
void ejbLoad() throws EJBException, RemoteException {
}
public
void ejbStore() throws EJBException, RemoteException {
}
/**
* Getter for CMP Field code
*
* @ejb.pk-field
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getCode();
/**
* Setter for CMP Field code
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setCode(String value);
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
String ejbCreate(String code) throws javax.ejb.CreateException {
setCode(code);
setDescription(code);
return
null;
}
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
String ejbCreate(String code, String description) throws
javax.ejb.CreateException {
setCode(code);
setDescription(description);
return
null;
}
/**
* Post Create method
*/
public
void ejbPostCreate(String code) throws javax.ejb.CreateException {
}
/**
* Post Create method
*/
public
void ejbPostCreate(String code, String description) throws
javax.ejb.CreateException {
}
/**
* Getter for CMP Field description
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract String getDescription();
/**
* Setter for CMP Field description
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setDescription(String value);
/**
* Setter for CMP Field description
*
* @ejb.interface-method view-type="local"
*/
public
ProductFamilyData getData() {
return
new ProductFamilyData(getCode(), getDescription());
}
}
Aucun nouveau tag n’a été utilisé ici.
6.3.5.Product
Voici le code du bean :
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
javax.ejb.EJBException;
import
javax.ejb.EntityBean;
import
javax.ejb.EntityContext;
import
javax.ejb.RemoveException;
import
com.society.stockmanager.interfaces.ProductFamilyLocal;
import
com.society.stockmanager.vo.ProductData;
/**
*
@ejb.bean name="Product"
*
display-name="Name for Product"
*
description="Description for Product"
*
local-jndi-name="ejb/Product"
*
type="CMP"
*
cmp-version="2.x"
*
view-type="local"
*
primkey-field = "id"
*
reentrant = "false"
*
schema = "Product"
*
*
@ejb.pk class = "java.lang.Integer"
*
*
@ejb.finder query = "select Object(p) from Product p"
*
result-type-mapping = "Local"
*
signature = "java.util.Collection findAll()"
*
*
@ejb.finder query = "select Object(p) from Provider p1,
IN(p1.products) p WHERE p1.city.country.countryId = ?1"
*
result-type-mapping = "Local"
*
signature = "java.util.Collection
findAllByCountryId(java.lang.Integer countryId)"
*
*
@jboss.persistence create-table = "true" table-name =
"Product" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL" remove-table = "true"
*
*/
public
abstract class ProductBean implements EntityBean {
public
ProductBean() {
super();
}
public
void setEntityContext(EntityContext ctx)
throws
EJBException,
RemoteException
{
}
public
void unsetEntityContext() throws EJBException, RemoteException {
}
public
void ejbRemove()
throws
RemoveException,
EJBException,
RemoteException
{
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
public
void ejbLoad() throws EJBException, RemoteException {
}
public
void ejbStore() throws EJBException, RemoteException {
}
/**
* Getter for CMP Field id
*
* @ejb.pk-field
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract java.lang.Integer getId();
/**
* Setter for CMP Field id
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setId(java.lang.Integer value);
/**
* Getter for CMP Field designation
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract java.lang.String getDesignation();
/**
* Setter for CMP Field designation
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setDesignation(java.lang.String value);
/**
* Getter for CMP Field description
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract java.lang.String getDescription();
/**
* Setter for CMP Field description
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setDescription(java.lang.String value);
/**
* Getter for CMP Field quantity
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public
abstract java.lang.Integer getQuantity();
/**
* Setter for CMP Field quantity
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setQuantity(java.lang.Integer value);
/**
* Create method
* @ejb.create-method view-type = "local"
*/
public
java.lang.Integer ejbCreate(
ProductData
datas,
ProductFamilyLocal
family) throws javax.ejb.CreateException {
setId(datas.getId());
setDescription(datas.getDescription());
setDesignation(datas.getDesignation());
setQuantity(datas.getQuantity());
return
null;
}
/**
* Post Create method
*/
public
void ejbPostCreate(ProductData datas, ProductFamilyLocal family)
throws
javax.ejb.CreateException {
setFamily(family);
}
/**
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "family"
* target-ejb = "ProductFamily"
* role-name = "product-productFamily"
* target-role-name = "productFamily-product"
* target-multiple = "yes"
* @jboss.relation related-pk-field="code"
fk-column="categoryCode" fk-constraint="true"
*/
public
abstract ProductFamilyLocal getFamily();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setFamily(ProductFamilyLocal value);
/**
* Getter for CMR Relationship
*
* @ejb.interface-method view-type="local"
* @ejb.relation name = "products-providers"
* role-name = "products-providers"
* target-ejb = "Provider"
* target-role-name = "providers-products"
* target-multiple = "yes"
*
*
@jboss.relation-mapping
*
style="relation-table"
*
*
@jboss.relation-table create-table = "true" remove-table =
"true" datasource = "java:/MySqlDS"
datasource-mapping = "mySQL"
*
table-name="productsProviders"
*
*
@jboss.relation
*
related-pk-field="providerId"
*
fk-column="providerId"
*
fk-constraint="true"
*
*
@jboss.target-relation
*
related-pk-field="id"
*
fk-column="productId"
*
fk-constraint="true"
*/
public
abstract java.util.Collection getProviders();
/**
* Setter for CMR Relationship
*
* @ejb.interface-method view-type="local"
*/
public
abstract void setProviders(java.util.Collection value);
/**
* Return a ProductData configured object
*
* @ejb.interface-method view-type="local"
*/
public
ProductData getData() {
return
new ProductData(getId(), getDesignation(), getDescription(),
getQuantity());
}
/**
* Set values of a ProductData configured object
*
* @ejb.interface-method view-type="local"
*/
public
void setData(ProductData datas) {
setDescription(datas.getDescription());
setDesignation(datas.getDesignation());
setQuantity(datas.getQuantity());
}
}
Ce bean est un peu plus complexe que les autres. En effet, il existe
une relation N-M entre Provider et Product, il faudra donc une table
de jointure au niveau de la base de données.
Nous fournissons, ici, l’ensemble des informations à
partir des tags xDoclet.
-
jboss.relation-mapping : permet de définir comment se
passe la relation entre deux ejbs (utilisé principalement
pour les relations many to many) :
-
style : prend seulement : « relation-table »
comme valeur
-
jboss.relation-table : définit la table de relation à
utiliser et différents paramètres concernant celle-ci
-
table-name : nom de la table de relation
-
datasource : nom JNDI du datasource dans lequel la table sera
créée
-
datasource-mapping : type de mapping à utiliser pour ce
datasource (mySQL, msSQL..)
-
create-table : création de la table lors du déploiement
(si celle-ci n’existe pas)
-
alter-table : modification de la structure de la table si les
propriétés de l’ejb ont changé
-
remove-table : suppression de la table lors du « undeploy »
de l’ejb
-
row-locking : utilisation de la syntaxe : SELECT …
FOR UPDATE au niveau des requêtes SQL SELECT
-
pk-constraint : indique si la contraite « clé
primaire » sera ajouté lors de la création
de la table
-
jboss.target-relation : même chose que jboss.relation
(mais utiliser pour les relations unidirectionnelles)
-
fk-constraint : utilisation de clé étrangère
pour les colonnes relationnelles
-
related-pk-field : nom de la clé primaire à
utiliser pour la partie «1-» de la relation
-
fk-column : nom de la colonne à utiliser pour la clé
étrangère
-
jdbc-type : type jdbc à utiliser
-
sql-type : type sql à utiliser
|
|
 |