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:
codice:
@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
codice:
@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;
}
}
mentre l'oggetto UtenteJpaController è un gestore che mi permette di creare e cancellare l'utente dal db
codice:
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:
codice:
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,java.lang.String) throws entityManager.exceptions.PreexistingEntityException,entityManager.exceptions.RollbackFailureException,java.lang.Exception
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,java.lang.String) throws entityManager.exceptions.PreexistingEntityException,entityManager.exceptions.RollbackFailureException,java.lang.Exception
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 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.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.ho allegato un file con le 3 classi java interessate
Grazie
Macimo