2 Replies Latest reply on Jan 21, 2010 7:34 AM by pravumishra

    what's a DAO

      Hi,

      I'm reading about DAO so after a long reaserch ,i think that  DAO is a session bean ,it is represented  by the class wich implements the interface .

      interface= contain all method that we can apply it to an Entity bean

      class=implements this interface and instanciates the Entity manager , and apply the method to relationnal tables

      is it right!!

      exemple:

       

      interface:

      package comet;

      import java.util.List;
      import javax.ejb.Remote;

      @Remote
      public interface ClientTestBeanRemote {
      public void addClient(Client var);
      public Client rechercherClient(int var);
      public void deleteClient(int var);
      public List<Client> listerTousLesClient();
      public Client updateClient(Client var1, Client var2);
      }

       

      the class:DAO

      package comet;

      import java.util.List;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      @Stateless
      public class ClientTestBean implements ClientTestBeanRemote {

      @PersistenceContext
      private EntityManager em; //L'Entity Manager

      public void addClient(Client var) {
        em.persist(var);
      }

      public Client rechercherClient(int var) {
        return em.find(Client.class, var);
      }

      public void deleteClient(int var) {
        Client resReq = rechercherClient(var);
        if (resReq != null) {
         em.remove(resReq);
        }
      }

      public List<Client> listerTousLesClient() {
        return em.createQuery("SELECT e FROM Client  e").getResultList();
      }

      public Client updateClient(Client var1, Client var2) {
        Client var = em.find(Client.class, var1.getId());
        if (var != null) {
         var.setId(var2.getId());

         var.setNumcarte(var2.getNumcarte());

         var.setNom(var2.getNom());

         var.setPrenom(var2.getPrenom());

        }

        return var;
      }

      }

      Is it right ,or not !!

      THANK YOU FOR HELP

        • 1. Re: what's a DAO
          elmaroufy

          DAO is not necessarily a session bean, it may be a simple spring bean for example.
          for the method updateClient, I do not think it will update the client :

           

          public Client updateClient(Client client) {

               Client updatedClient = em.merge(client);

               return updatedClient;

          }

           

          regarding deleteClient, it can be implemented like this :

           

          public void deleteClient(Client client) {
                em.remove(em.merge(client));  
          }

          • 2. Re: what's a DAO

            DAO(Data Access Object) is a design patteren and you may get more information about it from internet.

             

            It may have any kind of implementation by using plain JDBC, hibernate, entity bean , spring-hibernate or any kind of data access API currently available or comming in future.

             

            Thanks and Regards,