控制器中的事务中止

时间:2018-11-02 23:44:40

标签: jpa jsf jta

我在用Java ee 7构建的WebApp中实现JPA方法时遇到麻烦,它向我返回“ EJBException:事务异常中止”,但是参数“ ID”和“描述”已正确发送。

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测试的正确参数,但是我不知道为什么它不起作用,谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

  

但查询来自especialidadFacade上创建的JPA标准   与especialidaEntity

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

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