entity ejb

19
Stéphane Frenot - Département Télécommunication - S ID - [email protected] II - EjbEnt 1 Entity EJB

Upload: tymon

Post on 18-Jan-2016

26 views

Category:

Documents


0 download

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

TRANSCRIPT

Page 1: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - EjbEnt 1

Entity EJB

Page 2: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 3: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 4: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 5: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 6: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 7: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 8: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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 ?

Page 9: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 10: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 11: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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;

Page 12: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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%)

Page 13: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 14: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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

Page 15: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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)...

}

Page 16: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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;

}

Page 17: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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(){}

...

Page 18: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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){}

Page 19: Entity EJB

Stéphane Frenot - Département Télécommunication - SID - [email protected]

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.