6 Replies Latest reply on Jun 6, 2006 12:13 PM by Chris Baxter

    Web Services EJB3

    Ramzi Maalej Newbie

      How should I do to get a reference of EntityManager from my web services class to persist my EJB3 entity bean.

        • 1. Re: Web Services EJB3
          Ramzi Maalej Newbie

          I tried to store data into database through web service call to EntityManager but it doesn't store data in Mysql. here is my source code.

          Source code of web service :

          package webServices.utilisateur.service;
          import global.SOAP.StringSOAP;
          import global.SOAP.UtilisateurSOAP;
          import global.id.KeyGenerate;
          import global.outil.ArrayToVector;
          import global.outil.Converter;
          import global.outil.VectorToArray;
          import interfaceBean.UtilisateurInterface;
          import java.rmi.RemoteException;
          import java.util.Vector;
          import javax.persistence.EntityManager;
          import javax.persistence.EntityNotFoundException;
          import data.UtilisateurBean;
          import java.util.Properties;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
           * @author Ntim
          public class UtilisateurServiceImpl implements UtilisateurServiceSEI {
           private Properties properties;
           private Context context;
           protected EntityManager utilisateurManager;
           protected UtilisateurBean utilisateur;
           public UtilisateurServiceImpl() {
           properties = new Properties();
           try {
           context = new InitialContext(properties);
           utilisateurManager = (EntityManager) context.lookup("java:/EntityManagers/NtimBizEntityManager");
           } catch (NamingException ex) {
           //utilisateurManager = emf.createEntityManager();*/
           public StringSOAP ajouterUtilisateur(UtilisateurSOAP listeUtilisateur) throws RemoteException {
           System.out.println("Appel reussi du web service");
           //Extraire l'ensemble de son enveloppe
           UtilisateurInterface[] listeUtilisateurAjout = listeUtilisateur.getListeUtilisateur();
           Vector<String> utilisateurNonInseres = new Vector<String>();
           UtilisateurInterface utilisateurTransfereInterface = new UtilisateurInterface();
           UtilisateurBean utilisateurTransfereBean = new UtilisateurBean();
           for(int i = 0 ; i < listeUtilisateurAjout.length; i++) {
           //Extraire de la liste
           utilisateurTransfereInterface = (UtilisateurInterface)listeUtilisateurAjout;
           //convertir l'element
           utilisateurTransfereBean = Converter.utilisateurInterfaceToBean(utilisateurTransfereInterface);
           //Serealise l'element
           } catch (Exception eAjoutEchoue) {
           //Remettre le resultat dans son enveloppe
           StringSOAP listeUtilisateurNonInseres = new StringSOAP(VectorToArray.Vector2StringArray(utilisateurNonInseres));
           return listeUtilisateurNonInseres;

          here my persistence.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
           <persistence-unit name="NtimBizApp-ejb" transaction-type="RESOURCE_LOCAL">
           <property name="jboss.entity.manager.jndi.name" value="java:/EntityManagers/NtimBizEntityManager"/>
           <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagers/NtimBizEntityManagerFactory"/>
           <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

          • 2. Re: Web Services EJB3
            Pablo Javier Santa Cruz Cosp Newbie

            You can put in your class, this annotation along with a PersistenceManager reference variable:

            protected EntityManager em;

            • 3. Re: Web Services EJB3
              Chris Baxter Newbie

              I believe that annotation only works with an EJB3 Session Bean and it looks like they wanted to do this work in a POJO endpoint. When you use the EntityManager in a Session Bean that is properly setup to do container managed transactions, it (the EntityManager) is automatically flushed. I think your problem stems from either not having a Transaction or not explicitly flushing (comitting) the EntityManager. I've always used the EntityManager in a SessionBean, so I can't help you much beyond that.

              • 4. Re: Web Services EJB3
                Ramzi Maalej Newbie

                Thanks for you, I resolved the problem with adding a new layer between data and web service layer, this layer contains the session bean, that will allow me to persist data. I think that entity manager doesnt work in the context of web service. So why i added the session bean.

                • 5. Re: Web Services EJB3
                  Pablo Javier Santa Cruz Cosp Newbie

                  I am glad you solved the problem. The other thing you can do is expose your WebService as a EJB3 or EJB2.1 Stateless Session Bean directly.

                  Hope this helps. Regards.

                  • 6. Re: Web Services EJB3
                    Chris Baxter Newbie

                    You absolutely can use the EntityManager outside of EJBs, but if you do, you have to do a little extra work surrounding transactions. Check out the following link from the Hibernate EntityManager documentation to see how to explicitly commit the EntityManagers transactions when not using it inside of container managed transactions (in EJBs). Hope you find it helpful.