1 / 19

Entity EJB

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.

tymon
Download Presentation

Entity EJB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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

  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

  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

  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

  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

  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

  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 public public 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

  10. Création d'un EJB entité CMP Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  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

  12. Expression de recherche • Syntaxe LISP-like opé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

  13. Ecriture de la classe du Bean • La classe doit implanter l'interface EntityBean public 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

  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

  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

  16. Interface Home et PK classe AlarmClock primary Key Class: public class AlarmClock implements Serializable { pulic int idAlarm; } AlarmClock interface Home public 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

  17. La classe du bean public 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

  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

  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. Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

More Related