事务在控制器中中止

Transaction aborted in controller

我在 Java ee 7 内置的 WebApp 中实现 JPA 方法时遇到问题,它返回了我 "EJBException : Transaction aborted" ,但是参数 "ID" 和 "Description" 有发送正确。

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();

}

这很重要,在控制器上

    /*
     * 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;
        }


    }

正如我所说,JSF 向我发送了我用 FacesContext.addMessage 测试的正确参数,但我不知道为什么它不起作用,在此先感谢。

如果一个SystemException(系统异常必须是一个子类 的 RuntimeException 或 java.rmi.RemoteException) 被抛出,当前事务将被回滚

有关 EJB 中异常类型的更多信息,您可以查看 this link。

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

请看JPA: Weird error when I try to persist an object

在您的情况下,"desc" 是保留关键字