slide1 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
EJB3 PowerPoint Presentation
Download Presentation
EJB3

Loading in 2 Seconds...

play fullscreen
1 / 28

EJB3 - PowerPoint PPT Presentation


  • 171 Views
  • Uploaded on

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)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'EJB3' - scarlett


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
EJB3

Struts

ejb 3

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.

description in weblang
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=?"      }

}

use of an ejb3
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();

use of an ejb35
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();

use of an ejb36
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();

slide8
EJB3

Classes generated by WebLang

The files shown on the following slides are generated by the templates defined in the WebLang environment

ejb3 support functions manager
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() { …}

}

ejb3 the manager is created by a factory
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);

}

ejb3 support functions primary key
EJB3: support functions, primary key

// In the EJB class

privatelong id;

@Id

@GeneratedValue

publiclong getId() {

return id;

}

publicvoid setId(long id) {

this.id = id;

}

ejb3 support functions creation
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);

}

ejb3 support functions finding them
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());

} }

ejb3 support functions relationships
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

}

ejb3 the ejb is disconnected from the database between two calls
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();
ejb3 lazy evaluation
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
ejb3 reattaching the beans to the db
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();
the ejb3 avoid the need of transferring data from the persistent object to the servlet java beans
The EJB3 avoid the need of transferring data from the persistent object to the servlet Java beans
jet java emitter templates eclipse feature
JET: Java Emitter Templates (Eclipse feature)

aaa = xxx

bbb = yyy1, yyy2

ccc = vvv

ddd = zzz

aaa

bbb

ccc ddd

xxx

yyy1

yyy2

vvv zzz

jet java emitter templates
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

jet eclipse templates variable part
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;

<% }%>

}

jet eclipse templates constant part
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;

<% }%>

}

the templates defined in weblang can easily be modified by the developer
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=?"

} }

weblang templates
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

getting and compiling weblang templates
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)

use of the modified templates
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.

if you need extra parameters you may enter a block like this one
If you need extra parameters, you may enter a block like this one

parameters {

xxxx = "aa ss""bbb" ccc;

mainHtml = "/start/Lotery.jsp";

}

use of the extra parameters
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