Beruflich Dokumente
Kultur Dokumente
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.
1
Description in WebLang
ejb3 Country {
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=?"
}
}
Use of an EJB3
javax.persistence.EntityManager em = ejb3_utility.Manager.open();
javax.persistence.EntityTransaction tx = em.getTransaction();
tx.begin();
tx.commit(); // or tx.rollback();
ejb3_utility.Manager.close();
2
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 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();
3
EJB3: Expression of relationships in WebLang
indication in Xxxx in Yyyy methods available in Xxxx
<hasY 1:1 hasX> Yyyy no relation Yyyy getHasY()
<hasX 1:1 hasY> Xxxx setHasY(yyyy)
<hasX 1:1 target hasY> (setHasY is made from the forward
Xxxx relationship name with first letter
capitalized)
<hasY 1:1 target hasX> no relation Yyyy getHasY()
Yyyy <hasX 1:1 hasY> Xxxx setHasY(yyyy)
<hasY 1:N hasX> Yyyy no relation java.util.Collection y = getHasY()
<hasX N:1 hasY> setHasY(collection)
addHasY(yyyy), removeHasY(yyyy)
<hasY N:M hasX> no relation java.util.Collection y = getHasY()
Yyyy <hasX N:M hasY> setHasY(collection)
Xxxx addHasY(yyyy), removeHasY(yyyy)
EJB3
4
EJB3: support functions, manager
public class Manager { // ThreadLocal object, always the same
static private EntityManagerFactory emf = null;
private EntityManager em = null;
private static ThreadLocal<Manager> threadManager =
new ThreadLocal<Manager>() {
protected synchronized Manager initialValue() { … }
};
public static EntityManager open() { .. Next slide … }
public static void set(EntityManager em) { … }
public static void close() { … }
}
public static 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);
}
5
EJB3: support functions, primary key
6
EJB3: support functions, finding them
/**
* @generated
*/
public static 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) {
throw new javax.ejb.FinderException(enf.getMessage());
} }
7
EJB3: the EJB is disconnected from the
database between two calls
tx.begin();
client = Client.findByName("xxx");
tx.commit();
tx.begin();
// x is reattached to the transaction
x = em.merge(client);
tx.begin();
client = Client.findByName("xxx");
client.getHasBills().size(); // forces the collection to be loaded
// prevents the lazy evaluation
tx.commit();
8
EJB3: reattaching the beans to the DB
tx.begin();
9
JET: Java Emitter Templates
(Eclipse feature)
aaa = xxx
bbb = yyy1, yyy2
ccc = vvv
ddd = zzz
aaa xxx
bbb yyy1
ccc ddd yyy2
vvv zzz
JET
Java Emitter Templates
WebLang page :
http://ltiwww.epfl.ch/WebLang/Modules.html#modulestdm
10
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“ %>
11
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 struts Supplier {
package store;
statemachine {
state state_0 > supply;
switch (sessionState) {
case state_s:
sessionState = state_0;
break;
case state_0:
...
}
@templatedir = zzzz
This page is handled
by an updated template page supply {
country (Enter) {}
country.hasTowns[] {}
}
}
12
Getting and compiling WebLang templates
13
If you need extra parameters, you
may enter a block like this one
parameters {
xxxx = "aa ss" "bbb" ccc;
mainHtml = "/start/Lotery.jsp";
}
// html is the package and Html.class is the class. Also exist servlet.Servlet,
// struts.Struts
// See the WebLang page
14