0

I'm having troubles implementing JPA methods in my WebApp built in Java ee 7, its returned me "EJBException : Transaction aborted" , but the parameters "ID" and "Description" had send correctly.

package entities;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Fede-Frost
 */
@Entity
@Table(name = "especialidad")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Especialidad.findAll", query = "SELECT e FROM Especialidad e")
    , @NamedQuery(name = "Especialidad.findById", query = "SELECT e FROM Especialidad e WHERE e.id = :id")
    , @NamedQuery(name = "Especialidad.findByDesc", query = "SELECT e FROM Especialidad e WHERE e.desc = :desc")})
public class Especialidad implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Size(max = 45)
    @Column(name = "desc")
    private String desc;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "especialidad")
    private List<Medico> medicoList;

    public Especialidad() {
    }

    public Especialidad(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @XmlTransient
    public List<Medico> getMedicoList() {
        return medicoList;
    }

    public void setMedicoList(List<Medico> medicoList) {
        this.medicoList = medicoList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Especialidad)) {
            return false;
        }
        Especialidad other = (Especialidad) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "MyHospital.Especialidad[ id=" + id + " ]";
    }

}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entities.Estudiolab;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Fede-Frost
 */
@Stateless
public class EstudiolabFacade extends AbstractFacade<Estudiolab> implements EstudiolabFacadeLocal {

    @PersistenceContext(unitName = "MyHospital-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EstudiolabFacade() {
        super(Estudiolab.class);
    }

}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entities.Especialidad;
import java.util.List;
import javax.ejb.Local;

/**
 *
 * @author Fede-Frost
 */
@Local
public interface EspecialidadFacadeLocal {

    void create(Especialidad especialidad);

    void edit(Especialidad especialidad);

    void remove(Especialidad especialidad);

    Especialidad find(Object id);

    List<Especialidad> findAll();

    List<Especialidad> findRange(int[] range);

    int count();

}

And here it's the big deal, on the controller

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package controller;

import entities.Especialidad;
import javax.inject.Named;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import model.EspecialidadFacadeLocal;

    /**
     *
     * @author Fede-Frost
     */
    @ViewScoped
    @Named(value = "mediCon")


    public class mediCon implements Serializable {

        @EJB
        private EspecialidadFacadeLocal especFac;

        @Inject
        private Especialidad espec;            

        /**
         * Creates a new instance of MediCon
         * @return 
         */

        public void addSpec() {
            try {            
                especFac.create(espec);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Exito", "Se registro correctamente"));
            } catch (EJBException e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Aviso", e.toString() ));
            }

        }

        public Especialidad getEspec() {
            return espec;
        }

        public void setEspec(Especialidad espec) {
            this.espec = espec;
        }


    }

As I said, JSF sent me the correct parameters which I tested with FacesContext.addMessage, but I don't know why it doesn't work, thanks in advance.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    1. Versions of libraries =? 2. Stacktrace of the error (including "caused by..." nested exceptions). 3. Steps to reproduce the error =? (Can you simplify your sample and complete so it can be run by a 3rd party? So that it is reproducible.) – Konstantin Kolinko Nov 02 '18 at 23:55
  • If jsf sent the right parameters, it did its work and the question is not jsf related then. And what @KonstantinKolinko means is create a [mcve] – Kukeltje Nov 03 '18 at 07:53
  • I'm using JSF 2.2 javaxfaces, primefaces 5.0 and jdbc connector 5.1.23; I don't know how to make a Stacktrace of the error; the steps are simple, I'm trying to persist one entity and sending parameters through JSF. Today I'm testing and suddenly my Login() broke too, the same error, but yesterday works perfectly. – Fede Frost Nov 03 '18 at 12:17
  • Hi, thanks for the advice. Now I catch my exception and this is the complete description. Exception told me that I had an error on my query, but the query it's from JPA standard created on especialidadFacade with especialidaEntity, this is and Image from the complete exception. Image : imgur.com/SZJe3Jh – Fede Frost Nov 04 '18 at 18:28

1 Answers1

0

If a SystemException(A system exception must be a subclass of a RuntimeException or java.rmi.RemoteException) is thrown, the current transaction will be rollbacked

For more information about the Exception types in EJB, you could have a look at this link.

but the query it's from JPA standard created on especialidadFacade with especialidaEntity

please have a look at JPA: Weird error when I try to persist an object

in your case, "desc" is a reserved keyword

Mehran Mastcheshmi
  • 785
  • 1
  • 4
  • 12
  • Hi, thanks for the advice. Now I catch my exception and this is the complete description. Exception told me that I had an error on my query, but the query it's from JPA standard created on especialidadFacade with especialidaEntity, this is and Image from the complete exception. Image : https://imgur.com/SZJe3Jh – Fede Frost Nov 04 '18 at 18:27