Entity EJB

Preview:

DESCRIPTION

Entity EJB. Qu'est ce qu'un entity EJB. Il présente les comportements suivants : c'est la représentation de données persistantes ils survivent à un crash du SA plusieurs clients l'instance EJB contient une copie des données du système de persistance. Gestion de la persistance. - PowerPoint PPT Presentation

Citation preview

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 1

Entity EJB

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 2

Qu'est ce qu'un entity EJB

• Il présente les comportements suivants :– c'est la représentation de données persistantes– ils survivent à un crash du SA– plusieurs clients– l'instance EJB contient une copie des données

du système de persistance

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 3

Gestion de la persistance

• Les attributs de l'objet doivent être stockés sur un support de persistance

• Systèmes de persistance :– Sérialisation– Mapping SGBD à travers JDBC

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 4

EJB Entité partagée

• Quand plusieurs clients partagent le même EJB– Ils reçoivent leur propre instance d'EJB– Partagent les données– N'ont pas à gérer la synchronisation

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 5

Clés Primaires

• Chaque EJB Entité possède un ensemble d'attributs qui sont uniques lorsqu'ils sont agrégés

• Ces attributs agrégés s'appellent la clé primaire

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 6

Mapping Objet <->Relationnel

• Le mapping stocke chaque objet dans une seule ligne d'une table

• Une colonne de la base est associée à chaque attribut de la classe

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 7

EJB entité identiques

• Deux entités sont identiques si le contenu de leur clé primaire sont identiques

• Persistance :– CMP : Container Managed– BMP : Bean Managed

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 8

Différence entre le dvlp Entity / Session

• Il faut écrire une classe représentant la PK

• L'interface Home présente une méthode findByPrimaryKey(<PrimaryKey>)

• L'interface Home peut présenter d'autres méthodes find

• La classe du bean implémente l'interface javax.ejb.EntityBean

• La classe du bean doit fournir une méthode ejbCreate ET une méthode ejbPostCreate() pour chaque méthode create de l’interface home.– ==> A quoi sert le postCreate ?

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 9

PK Class

• Une classe de clé primaire– est déclarée public– implante serializable– possède un constructeur sans argument– présente tous ses attributs publicpublic class AlarmClockPK implements java.io.Serializable

{

public int serialNumber;

public string currentStation;

}

• NB : Les attributs de la PK doivent se retrouver dans le bean

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 10

Création d'un EJB entité CMP

C lient

EJBHom e EJBO bject SessionContext Synchronisation instance

create(args)e jbCreate(args)

extraction des cham ps gérés par le conta iner

creation de la représentation de l'entité dans la base

new

ejbPostC reate(args)

Base dedonnées

Pk

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 11

Interfaces Home

• Les interfaces Home doivent inclure des méthodes de recherche

public <RemoteIF> findByPrimaryKey(<PrimaryKey> pk)

throws FinderException, RemoteException;

• Les autres méthodes :public <RemoteIF> findNimporteComment(...)

throws FinderException, RemoteException;

public Enumeration findNimporteComment(...)

throws FinderException, RemoteException;

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 12

Expression de recherche

• Syntaxe LISP-likeopérateur opérande1 opérande2

• Opérateurs disponibles– (), =, <, <=, >, >=, !, &, |, like

• Opérandes possibles– 1) une autre expression– 2) un attribut d'un EJB– 3) un paramètre récupéré dans l ’invocation du find

• Exemple :

(> balance $amount)

(& (> bal $amount) (!(=accountType ‘ checking ’)))

(= 1 1)

(like lastName M%)

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 13

Ecriture de la classe du Bean

• La classe doit implanter l'interface EntityBeanpublic interface EntityBean extends EnterpriseBean {

public void ejbActivate ();

public void ejbPassivate();

public void ejbLoad();

public void ejbStore();

public void setEntityContext(EntityContext ctx);

public void unsetEntityContext ();

public void ejbRemove ();

}

• Autres points– Les méthodes ejbCreate retournent void pour les CMP– Il n’est pas nécessaire de développer des méthodes find pour les CMP

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 14

Exemple

• Un système de paramétrage de reveil

• L ’EJB enregistre– L’heure de l’alarme– La station sélectionnée– La bande sélectionnée

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 15

Interface Remote

public interface AlarmClock extends EJBObject {

public Date getAlarmTime() throws RemoteException;

public String getRadioStation() throws ...;

public boolean isFmSet() throws...;

public void isFmset(boolean fm) ...

public void setAlarmTime(Date time) ...;

... setRadioStation(String station)...

}

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 16

Interface Home et PK classe

AlarmClock primary Key Class:

public class AlarmClock implements Serializable {

pulic int idAlarm;

}

AlarmClock interface Homepublic interface AlarmClockHome extends EJBHome{

AlarmClock create()

throws CreateException, RemoteException;

AlarmClock create(Date time, String station, boolean fm)

throws CreateException, RemoteException;

AlarmClock findByPrimaryKey(AlarmClock id)

throws FinderException, RemoteException;

Enumeration findAllFMSettings()

throws FinderException, RemoteException;

}

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 17

La classe du beanpublic class AlarmClockBean implements EntityBean{

public int idAlarm;

transient protected EntityContext context;

public String station;

public String wakeUpTime;

public boolean isFm;

public void ejbActivate(){}

public void ejbLoad(){}

public void ejbPassivate(){}

public void ejbRemove(){}

public void ejbStore(){}

...

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 18

La suite de la classe du Bean...

this.idAlarm=(int) (Math.random()*100000);

Properties props=context.getEnvironment();

this.station=props.getProperty("Station");

this.isFm=props.getProperty("isFM");

this.wakeUpTime=props.getProperty("Time");

public void ejbCreate(String time, String station, boolean isFM){

this.idAlarm=(int)(Math.Random()*100000);

this.wakeUpTime=time;

this.station=station;

this.isFM=new Boolean(isFM).toString();

}

public void ejbPostCreate(){}public void ejbPostCreate(String time, String station, boolean isFm){}

Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

II - EjbEnt 19

Le cas des BMP

• Il faut coder soit même la gestion de la persistance pour les méthodes ejbCreate, ejbRemove, ejbLoad, ejbStore

• La méthode ejbCreate renvoie dans ce cas une instance de la classe PK

• les méthodes ejbFind doivent être codées.

Recommended