IALweb Homepage
Forum Home Forum Home > Programmazione > Programmazione > JAVA
  New Posts New Posts RSS Feed - problema inserimento dati nel db
  FAQ FAQ  Forum Search   Events   Register Register  Login Login


REGISTRATEVI su IALWeb forum!

Topic Closedproblema inserimento dati nel db

 Post Reply Post Reply
Author
Message
macimo View Drop Down
Nuovo Utente
Nuovo Utente


Joined: 18/Set/2009
Status: Offline
Points: 1
Direct Link To This Post Topic: problema inserimento dati nel db
    Posted: 18/Set/2009 at 15:50
Ciao,

sto svolgendo il progetto,ed avrei un problema nell'inserimento dei dati nel database.
Ho creato un bottone "salva" che una volta cliccato (mediante web services) viene richiamata la seguente classe:

package session;

import entityManager.exceptions.PreexistingEntityException;
import entityManager.exceptions.RollbackFailureException;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ejb.Stateless;

import entity.Utente;
import entityManager.UtenteJpaController;

@WebService()
@Stateless()
public class SalvaUtenteWS
{
    @WebMethod(operationName = "salvaUtente")
    public String salvaUtente (
                                   @WebParam(name = "nome")String nome,
                                   @WebParam(name = "cognome")String cognome,
                                   @WebParam(name = "username")String username,
                                   @WebParam(name = "password")String password,
                                   @WebParam(name = "confPassword")String confPassword,
                                   @WebParam(name = "email")String email
                                ) throws PreexistingEntityException,RollbackFailureException, Exception
    {
        if(nome.equals("") || cognome.equals("") || username.equals("") || password.equals("") || email.equals(""))
        {
             System.out.println("errore inserimento dati");
             return "fallimento";
        }
        else if(password.equals(confPassword))
        {
            
             Utente u=new Utente();
             System.out.println("Ho creato un nuovo utente");

             u.setNome(nome);
             System.out.println("inserisco il nome nel db");

             u.setCognome(cognome);
             System.out.println("inserisco il cognome nel db");
            
             u.setUsername(username);
             System.out.println("inserisco username nel db");
            
             u.setPassword(password);
             System.out.println("inserisco la password nel db");
            
             u.setEmail(email);
             System.out.println("inserisco la mail nel db");

             UtenteJpaController umi=new UtenteJpaController();
             umi.create(u);
             System.out.println("inserisco l'utente nel db");

             return "successo";
        }
        else
        {
             System.out.println("errore password");
             return "fallimento";
        }
    }
}

dove l'oggetto Utente Ŕ un entity bean corrispondente ad una tabella

package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "UTENTE")
@NamedQueries({@NamedQuery(name = "Utente.findAll", query = "SELECT u FROM Utente u"), @NamedQuery(name = "Utente.findByPassword", query = "SELECT u FROM Utente u WHERE u.password = :password"), @NamedQuery(name = "Utente.findByNome", query = "SELECT u FROM Utente u WHERE u.nome = :nome"), @NamedQuery(name = "Utente.findByCognome", query = "SELECT u FROM Utente u WHERE u.cognome = :cognome"), @NamedQuery(name = "Utente.findByUsername", query = "SELECT u FROM Utente u WHERE u.username = :username"), @NamedQuery(name = "Utente.findByEmail", query = "SELECT u FROM Utente u WHERE u.email = :email")})
public class Utente implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "PASSWORD")
    private String password;
    @Basic(optional = false)
    @Column(name = "NOME")
    private String nome;
    @Basic(optional = false)
    @Column(name = "COGNOME")
    private String cognome;
    @Basic(optional = false)
    @Column(name = "USERNAME")
    private String username;
    @Basic(optional = false)
    @Column(name = "EMAIL")
    private String email;

    public Utente() {
    }

    public Utente(String password) {
        this.password = password;
    }

    public Utente(String password, String nome, String cognome, String username, String email) {
        this.password = password;
        this.nome = nome;
        this.cognome = cognome;
        this.username = username;
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (password != null ? password.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Utente)) {
             return false;
        }
        Utente other = (Utente) object;
        if ((this.password == null && other.password != null) || (this.password != null && !this.password.equals(other.password))) {
             return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Utente[password=" + password + "]";
    }

}


mentre l'oggetto UtenteJpaController Ŕ un gestore che mi permette di creare e cancellare l'utente dal db


package entityManager;

import entity.Utente;
import entityManager.exceptions.NonexistentEntityException;
import entityManager.exceptions.PreexistingEntityException;
import entityManager.exceptions.RollbackFailureException;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;

public class UtenteJpaController {
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "EJBPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Utente utente) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             utx.begin();
             em = getEntityManager();
             em.persist(utente);
             utx.commit();
        } catch (Exception ex) {
             try {
                 utx.rollback();
             } catch (Exception re) {
                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
             }
             if (findUtente(utente.getPassword()) != null) {
                 throw new PreexistingEntityException("Utente " + utente + " already exists.", ex);
             }
             throw ex;
        } finally {
             if (em != null) {
                 em.close();
             }
        }
    }

    public void edit(Utente utente) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             utx.begin();
             em = getEntityManager();
             utente = em.merge(utente);
             utx.commit();
        } catch (Exception ex) {
             try {
                 utx.rollback();
             } catch (Exception re) {
                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
             }
             String msg = ex.getLocalizedMessage();
             if (msg == null || msg.length() == 0) {
                 String id = utente.getPassword();
                 if (findUtente(id) == null) {
                     throw new NonexistentEntityException("The utente with id " + id + " no longer exists.");
                 }
             }
             throw ex;
        } finally {
             if (em != null) {
                 em.close();
             }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             utx.begin();
             em = getEntityManager();
             Utente utente;
             try {
                 utente = em.getReference(Utente.class, id);
                 utente.getPassword();
             } catch (EntityNotFoundException enfe) {
                 throw new NonexistentEntityException("The utente with id " + id + " no longer exists.", enfe);
             }
             em.remove(utente);
             utx.commit();
        } catch (Exception ex) {
             try {
                 utx.rollback();
             } catch (Exception re) {
                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
             }
             throw ex;
        } finally {
             if (em != null) {
                 em.close();
             }
        }
    }

    public List<Utente> findUtenteEntities() {
        return findUtenteEntities(true, -1, -1);
    }

    public List<Utente> findUtenteEntities(int maxResults, int firstResult) {
        return findUtenteEntities(false, maxResults, firstResult);
    }

    private List<Utente> findUtenteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
             Query q = em.createQuery("select object(o) from Utente as o");
             if (!all) {
                 q.setMaxResults(maxResults);
                 q.setFirstResult(firstResult);
             }
             return q.getResultList();
        } finally {
             em.close();
        }
    }

    public Utente findUtente(String id) {
        EntityManager em = getEntityManager();
        try {
             return em.find(Utente.class, id);
        } finally {
             em.close();
        }
    }

    public int getUtenteCount() {
        EntityManager em = getEntityManager();
        try {
             return ((Long) em.createQuery("select count(o) from Utente as o").getSingleResult()).intValue();
        } finally {
             em.close();
        }
    }

}


Una volta che eseguo il tutto mi viene lanciata la seguente eccezione generata,secondo me,nel momento in cui si tenta di creare l'utente (umi.create(u)),infatti mi vengono stampate a video tutte le stringhe tranne l'ultima ("inserisco utente nel db").Questa Ŕ l'eccezione generata:


15:45:06,599 INFO  [TomcatDeployment] undeploy, ctxPath=/WebClient
15:45:07,258 INFO  [TomcatDeployment] deploy, ctxPath=/WebClient
15:46:27,803 WARN  [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from stateless bean context, it should already have been injected
15:46:27,819 WARN  [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
15:46:27,824 WARN  [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
15:46:27,831 WARN  [InterceptorRegistry] applicable interceptors is non-existent for public java.lang.String session.SalvaUtenteWS.salvaUtente(java.lang.String,java.lang .String,java.lang.String,java.lang.String,java.lang.String,j ava.lang.String) throws entityManager.exceptions.PreexistingEntityException,entityMa nager.exceptions.RollbackFailureException,java.lang.Exceptio n
15:46:27,846 WARN  [InterceptorRegistry] applicable interceptors is non-existent for public java.lang.String session.SalvaUtenteWS.salvaUtente(java.lang.String,java.lang .String,java.lang.String,java.lang.String,java.lang.String,j ava.lang.String) throws entityManager.exceptions.PreexistingEntityException,entityMa nager.exceptions.RollbackFailureException,java.lang.Exceptio n
15:46:27,861 INFO  [STDOUT] Ho creato un nuovo utente
15:46:27,861 INFO  [STDOUT] inserisco il nome nel db
15:46:27,861 INFO  [STDOUT] inserisco il cognome nel db
15:46:27,861 INFO  [STDOUT] inserisco username nel db
15:46:27,861 INFO  [STDOUT] inserisco la password nel db
15:46:27,861 INFO  [STDOUT] inserisco la mail nel db
15:46:27,908 INFO  [STDOUT] 1
15:46:27,908 INFO  [STDOUT] prima rollback
15:46:27,938 WARN  [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from stateless bean context, it should already have been injected
15:46:27,970 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
entityManager.exceptions.RollbackFailureException: An error occurred attempting to roll back the transaction.
        at entityManager.UtenteJpaController.create(UtenteJpaController .java:53)
        at session.SalvaUtenteWS.salvaUtente(SalvaUtenteWS.java:65)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(Method Invocation.java:122)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:111)
        at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBC ontainerInvocationWrapper.java:69)
        at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke( InterceptorSequencer.java:73)
        at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundI nvoke(InterceptorSequencer.java:59)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointA dvice.java:174)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor .fillMethod(InvocationContextInterceptor.java:72)
        at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.Invocat ionContextInterceptor_z_fillMethod_17064560.invoke(Invocatio nContextInterceptor_z_fillMethod_17064560.java)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor .setup(InvocationContextInterceptor.java:88)
        at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.Invocat ionContextInterceptor_z_setup_17064560.invoke(InvocationCont extInterceptor_z_setup_17064560.java)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor .invoke(CachedConnectionInterceptor.java:62)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.entity.TransactionScopedEntityManagerIntercep tor.invoke(TransactionScopedEntityManagerInterceptor.java:56 )
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOp erationsInterceptor.java:47)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.jav a:42)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke (StatelessInstanceInterceptor.java:68)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79 )
        at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterce ptor.java:190)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropa gationInterceptor.java:76)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.jav a:42)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptorv2. invoke(RoleBasedAuthorizationInterceptorv2.java:201)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invo ke(Ejb3AuthenticationInterceptorv2.java:186)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagati onInterceptor.java:41)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(A synchronousInterceptor.java:106)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(Bloc kContainerShutdownInterceptor.java:67)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.aspects.currentinvocation.CurrentInvocationInterce ptor.invoke(CurrentInvocationInterceptor.java:67)
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodIn vocation.java:102)
        at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(Stat elessContainer.java:372)
        at org.jboss.ejb3.stateless.StatelessContainer.invokeEndpoint(S tatelessContainer.java:727)
        at org.jboss.wsf.container.jboss50.invocation.InvocationHandler EJB3.invoke(InvocationHandlerEJB3.java:96)
        at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(Servi ceEndpointInvoker.java:219)
        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(R equestHandlerImpl.java:474)
        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(Re questHandlerImpl.java:295)
        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHa ndlerImpl.java:205)
        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpReques t(RequestHandlerImpl.java:131)
        at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service (AbstractEndpointServlet.java:85)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFi lter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(App licationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(Repl yHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFi lter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(App licationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(Standar dWrapperValve.java:235)
        at org.apache.catalina.core.StandardContextValve.invoke(Standar dContextValve.java:191)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invok e(SecurityAssociationValve.java:190)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccCo ntextValve.java:92)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentVa lve.process(SecurityContextEstablishmentValve.java:126)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentVa lve.invoke(SecurityContextEstablishmentValve.java:70)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHo stValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorRepo rtValve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invok e(CachedConnectionValve.java:158)
        at org.apache.catalina.core.StandardEngineValve.invoke(Standard EngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAd apter.java:330)
        at org.apache.coyote.http11.Http11Processor.process(Http11Proce ssor.java:829)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHand ler.process(Http11Protocol.java:601)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoin t.java:447)
        at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.NullPointerException
        at entityManager.UtenteJpaController.create(UtenteJpaController .java:50)
        ... 81 more


P.S.utilizzo netbeans e jboss

Grazie
Macimo
Back to Top
Sponsored Links


Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 10.17
Copyright ©2001-2013 Web Wiz Ltd.

This page was generated in 0,031 seconds.