事务在控制器中中止



我在用Java ee 7构建的WebApp中实现JPA方法时遇到问题,它返回了"EJBException:Transaction aborted",但参数"ID"one_answers"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测试的正确参数,但我不知道为什么它不起作用,提前谢谢。

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

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

但查询来自在specialidadFacade上创建的JPA标准具有特殊实体

请查看JPA:当我试图持久化对象时出现了奇怪的错误

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

相关内容

  • 没有找到相关文章

最新更新