-
1. Re: Web Services EJB3
ramcisjboss Jun 2, 2006 2:51 PM (in response to ramcisjboss)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(); properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); properties.put("java.naming.provider.url","localhost:1099"); try { context = new InitialContext(properties); utilisateurManager = (EntityManager) context.lookup("java:/EntityManagers/NtimBizEntityManager"); } catch (NamingException ex) { ex.printStackTrace(); } //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); try{ //Serealise l'element System.out.println(utilisateurTransfereBean.getRefObjet()); this.utilisateurManager.persist(utilisateurTransfereBean); } catch (Exception eAjoutEchoue) { utilisateurNonInseres.add((String)utilisateurTransfereInterface.getRefObjet()); } } //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"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:MySqlDS</jta-data-source> <properties> <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"/> </properties> </persistence-unit> </persistence>
-
2. Re: Web Services EJB3
pablojavierpy Jun 5, 2006 11:09 AM (in response to ramcisjboss)You can put in your class, this annotation along with a PersistenceManager reference variable:
@PersistenceContext(unitName="YOUR_UNIT_NAME") protected EntityManager em;
-
3. Re: Web Services EJB3
cbax007 Jun 5, 2006 6:49 PM (in response to ramcisjboss)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
ramcisjboss Jun 6, 2006 4:51 AM (in response to ramcisjboss)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
pablojavierpy Jun 6, 2006 7:40 AM (in response to ramcisjboss)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
cbax007 Jun 6, 2006 12:13 PM (in response to ramcisjboss)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.
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/transactions.html#transactions-basics