1 / 28

EJB3

EJB3. Struts. EJB 3. The EJB 3 provide an alternative to the CMP Entity Beans to access the database. Unlike the previous ones, they are synchronized with the database only when decided by the program. (See description in the WebLang page)

scarlett
Download Presentation

EJB3

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. EJB3 Struts

  2. EJB 3 The EJB 3 provide an alternative to the CMP Entity Beans to access the database. Unlike the previous ones, they are synchronized with the database only when decided by the program. (See description in the WebLang page) Book: D. Panda, R.Rahman, D Lane, “EJB3 in Action”, Hamming.

  3. Description in WebLang ejb3Country {packagemyPackage;relation<hasTowns 1:NisIn> Town;      String name;intnumber;publicCountry create(String name, intnumber);publicCountry find(String name) {            query = "select c from Country c where c.name=?"      } }

  4. Use of an EJB3 javax.persistence.EntityManager em = ejb3_utility.Manager.open();javax.persistence.EntityTransaction tx = em.getTransaction();tx.begin();Country c = Country.create("Switzerland", 1291);c.setNumber(1291);   // will appear in the DB at the latest at the commit  . . . Other calls to the EJBs in the same transaction . . .tx.commit();     // or tx.rollback();ejb3_utility.Manager.close();

  5. Use of an EJB3 javax.persistence.EntityManager em = ejb3_utility.Manager.open();javax.persistence.EntityTransaction tx = em.getTransaction();tx.begin(); try {country = Country.find("Switzerland"); } catch (javax.ejb.FinderException fe) {country = Country.create("Switzerland"); } tx.commit();     // or tx.rollback();ejb3_utility.Manager.close();

  6. Use of an EJB3 javax.persistence.EntityManager em = ejb3_utility.Manager.open();javax.persistence.EntityTransaction tx = em.getTransaction();tx.begin(); em.remove(country); tx.commit();     // or tx.rollback();ejb3_utility.Manager.close();

  7. EJB3: Expression of relationships in WebLang :

  8. EJB3 Classes generated by WebLang The files shown on the following slides are generated by the templates defined in the WebLang environment

  9. EJB3: support functions, manager publicclass Manager { // ThreadLocal object, always the same staticprivate EntityManagerFactory emf = null; private EntityManager em = null; privatestatic ThreadLocal<Manager> threadManager = new ThreadLocal<Manager>() { protectedsynchronized Manager initialValue() { … } }; publicstatic EntityManager open() { .. Next slide … } publicstaticvoid set(EntityManager em) { … } publicstaticvoid close() { …} }

  10. EJB3: the manager is created by a factory publicstatic EntityManager open() { // main manager method, always the same Manager manager = (Manager) threadManager.get(); if (manager.em == null) { if (Manager.emf == null) Manager.emf = Persistence.createEntityManagerFactory("TestEJB3"); manager.em = manager.emf.createEntityManager(); manager.em.setFlushMode(FlushModeType.COMMIT); } return (manager.em); }

  11. EJB3: support functions, primary key // In the EJB class privatelong id; @Id @GeneratedValue publiclong getId() { return id; } publicvoid setId(long id) { this.id = id; }

  12. EJB3: support functions, creation /** *@generated // used by the merger, if it is called */ publicstatic Country create(String name,int number) { Country entity = new Country(); entity.name = name; entity.number = number; javax.persistence.EntityManager em = ejb3_utility.Manager.open(); em.persist(entity); return (entity); }

  13. EJB3: support functions, finding them /** *@generated */ publicstatic Country find(String name) throws Exception { javax.persistence.EntityManager em = ejb3_utility.Manager.open(); Query query = em.createQuery("select c from Country c where c.name=?"); query.setParameter(1,name); try { Country result = (Country)query.getSingleResult(); return (result); } catch (javax.persistence.NoResultException enf) { thrownew javax.ejb.FinderException(enf.getMessage()); } }

  14. EJB3: support functions, relationships @OneToMany(mappedBy="isIn") public java.util.Collection<myPackage.Town> getHasTowns() { return (hasTowns); } publicvoid setHasTowns(java.util.Collection<myPackage.Town> hasTowns) { if (hasTowns!=null) { hasTowns.size(); // trick to avoid lazy initialisation } this.hasTowns = hasTowns;} publicvoid addHasTowns(myPackage.Town town) { if (this.hasTowns == null) this.hasTowns = new java.util.ArrayList<myPackage.Town>(); this.hasTowns.add(town); town._setIsIn(this); // sets the reverse link }

  15. EJB3: the EJB is disconnected from the database between two calls • tx.begin();    client = Client.findByName("xxx");tx.commit(); • . . . presentation layer (Struts) . . . • tx.begin(); • // x is reattached to the transaction •     x = em.merge(client);          for (Bill b: client.getHasBills() {        x.addHasBills(em.merge(b));    }tx.commit();

  16. EJB3: lazy evaluation • tx.begin();    client = Client.findByName("xxx");    client.getHasBills().size(); // forces the collection to be loaded • // prevents the lazy evaluationtx.commit(); • // the collection may be handled outside the transaction • // provided the lazy evaluation has been thwarted

  17. EJB3: reattaching the beans to the DB • tx.begin(); • // reattach x to the transaction •     x = em.merge(client);        // reintroduce the new copy into the session • request.getSession().setAttribute("client", x);   • // attach the member of the collection    for (Bill b: client.getHasBills() {        x.addHasBills(em.merge(b));    }tx.commit();

  18. The EJB3 avoid the need of transferring data from the persistent object to the servlet Java beans

  19. JET: Java Emitter Templates (Eclipse feature) aaa = xxx bbb = yyy1, yyy2 ccc = vvv ddd = zzz aaa bbb ccc ddd xxx yyy1 yyy2 vvv zzz

  20. JETJava Emitter Templates Support derived from the JSP compiler to create templates in many different languages WebLang page : http://ltiwww.epfl.ch/WebLang/Modules.html#modulestdm

  21. JET: Eclipse templates, variable part <%@ jet package="ch.epfl.lti.codegen.weblang.template.java.client.rmi" imports="ch.epfl.lti.codegen.weblang.modules.java.client.rmi.* ch.epfl.lti.codegen.weblang.modules.java.base.*" class="GenRmiInterface“ %> <% Rmi rmi = (Rmi)_argument[0]; String CLASSNAME = rmi.getNameFU(); %> /* The interface of RMI method */ <% if (rmi.getPackage() != null) {%> package <%=rmi.getPackage().toString() %>; <% }%> import java.rmi.Remote; public interface <%=CLASSNAME%> extends Remote { <% for (String signature : rmi.getMethodSignature()) {%> public <%=signature%> throws Exception; <% }%> }

  22. JET: Eclipse templates, constant part <%@ jet package="ch.epfl.lti.codegen.weblang.template.java.client.rmi" imports="ch.epfl.lti.codegen.weblang.modules.java.client.rmi.* ch.epfl.lti.codegen.weblang.modules.java.base.*" class="GenRmiInterface" skeleton="../generator.skeleton" %> <% Rmi rmi = (Rmi)_argument[0]; String CLASSNAME = rmi.getNameFU(); %> /* The interface of RMI method */ <% if (rmi.getPackage() != null) {%> package <%=rmi.getPackage().toString() %>; <% }%> import java.rmi.Remote; public interface <%=CLASSNAME%> extends Remote { <% for (String signature : rmi.getMethodSignature()) {%> public <%=signature%> throws Exception; <% }%> }

  23. The templates defined in WebLang can easily be modified by the developer // Handled by a modified template @templatedir = myEjb3 ejb3 Country extends strutsfsm.StateForm { package myPackage; relation <hasTowns 1:N isIn> Town; String name; int number; public Country create(String name, int number); public Country find(String name) { query = "select c from Country c where c.name=?" } }

  24. WebLang templates struts Supplier { package store; statemachine { state state_0 > supply; switch (sessionState) { case state_s: sessionState = state_0; break; case state_0: . . . } @templatedir = zzzz page supply { country (Enter) {} country.hasTowns[] {} } } This page is handled by an updated template

  25. Getting and compiling WebLang templates Right-click the.cg file containing the @templatedir Get Templates  folder _cg_resources with the templates corresponding to the module is created Right-click the template you have modified  Compile CG Template  a new project is created to contain the templates (starts with “.” and is thus not always visible)

  26. Use of the modified templates When you have compiled a template, it will replace the standard one for all subsequent compilations of the .cg file, as long as you keep the @templatedir mark.

  27. If you need extra parameters, you may enter a block like this one parameters { xxxx = "aa ss""bbb" ccc; mainHtml = "/start/Lotery.jsp"; }

  28. Use of the extra parameters <% LinkedList sList = (LinkedList)_argument[0]; // within the html template // sList is the list of parameters defined in the .cg file DataTree root = (DataTree)_argument[1]; // root of the tree created by the parser LinkedList aList = root.getMainInstancesOfClass(html.Html.class); // available in every template for (Object o : aList) { HashMap hm = ((html.Html)o).getParameters(); // one hash map per module // with one key and one linkedList of String // for each line of parameters String name = ((html.Html)o).getName(); // get the name of the module } %> // html is the package and Html.class is the class. Also exist servlet.Servlet, // struts.Struts // See the WebLang page

More Related