Ejb-enchainement d'un projet de j2ee

Disponible uniquement sur Etudier
  • Pages : 5 (1023 mots )
  • Téléchargement(s) : 0
  • Publié le : 28 mai 2010
Lire le document complet
Aperçu du document
EJB:traitement des classes
@Entity
public class Account implements Serializable{

private String name;
private int amount;
private Collection coOwner = new ArrayList();

public Account(String n, int s) {
super();
this.name = n;
this.amount = s;
this.setCoOwner(new Vector());
}

public Account(){
super();
}

@Id
public String getName(){
return this.name;
}

public voidsetName(String n){
this.name = n;
}

public int getAmount(){
return this.amount;
}

public void addCoOwner(CoOwner ass){
this.coOwner.add(ass);
}

public void setAmount(int amnt){
this.amount = amnt;
}

public void setCoOwner(Collection coOwner) {
this.coOwner = coOwner;
}

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch=FetchType.EAGER)
public CollectiongetCoOwner() {
return coOwner;
}
}

@Stateless
public class AccountSessionBean implements AccountSessionBeanRemote {

@PersistenceContext
private EntityManager em;

/**
* Default constructor.
*/
public AccountSessionBean() {
// TODO Auto-generated constructor stub
}

public void createAccount(String name, int amnt){
Account account = new Account(name,amnt);em.persist(account);
}

public int getBalance(String name){
Account account = em.find(Account.class, name);
return account.getAmount();
}

public void setBalance(String name, int amnt){
Account account = em.find(Account.class, name);
account.setAmount(amnt);
em.merge(account);
}

public ArrayList listAccount(){
List list = em.createQuery("SELECT p FROM Account p").getResultList();
ArrayListal = new ArrayList();
for(Account a: list){
al.add(a);
}
return al;
}

public void addCoOwner(String nameOwner, CoOwner assos){
em.persist(assos);
Account account = em.find(Account.class, nameOwner);
account.addCoOwner(assos);
em.merge(account);
}

public ArrayList getCoOwner(String nameOwner){
Account account = em.find(Account.class, nameOwner);
Collection associes =account.getCoOwner();
ArrayList list = new ArrayList();
for(CoOwner ass: associes){
list.add(ass);
}
return list;
}

}
@Remote
public interface AccountSessionBeanRemote {

public void createAccount(String name, int amnt);

public int getBalance(String name);

public void setBalance(String name, int amnt);

public ArrayList listAccount() ;

public void addCoOwner(String nameOwner, CoOwnerassos);

public ArrayList getCoOwner(String nameOwner);

}
@Entity
public class CoOwner implements Serializable{

private String name;
private String relationWithOwner;
private int id;

public CoOwner(){
super();
}

public CoOwner(String n){
this.name = n;
this.setRelationWithOwner("Other");
}

public CoOwner(String n, String relation){
this.name = n;this.setRelationWithOwner(relation);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() { return id; }
public void setId(int id) { this.id = id; }

public String getName(){
return name;
}

public void setName(String n){
this.name = n;
}

public void setRelationWithOwner(String relationWithOwner) {
this.relationWithOwner = relationWithOwner;
}

public StringgetRelationWithOwner() {
return relationWithOwner;
}

}

Bank:les actions:
public class ActionCreate extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String name = req.getParameter("name");
int amnt = Integer.parseInt(req.getParameter("amount"));
try {Action.getAccountSessionBean().createAccount(name, amnt);
res.sendRedirect("function.html");
} catch (Exception e) {
req.setAttribute("error",e);
req.getRequestDispatcher("error.jsp").forward(req, res);
}
}

}

public class ActionAddCoOwner extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws...
tracking img