嗨,所以我的标题中所述的删除方法有问题,出现错误的是javax.ejb.EJBException。有一个堆栈跟踪,组件树和范围变量选项卡,但有很多内容不确定我是否应该在这里发布它们?如果是这样,我会说。对不起,我不能隔离问题我认为如果可以的话我可以解决它,所以我会包括我认为相关的类。我尽力尽我所能地问这个问题。 感谢
好的,我的index.xhtml文件中有
<h:link value="add new cd" outcome="add"></h:link>
<h:form>
<h:dataTable var="p" value="#{cdController.findAll()}" border="1" cellpadding="2">
<h:column>
<f:facet name="header">ID</f:facet>
<h:outputText value="#{p.id}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Artist</f:facet>
<h:outputText value="#{p.artist}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Duration</f:facet>
<h:outputText value="#{p.duration}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
<h:outputText value="#{p.title}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Option</f:facet>
<h:commandLink value="Delete" action="#{cdController.delete(c)}"></h:commandLink>
</h:column>
</h:dataTable>
</h:form>
在我的控制器课程中,我有
package controller;
import entity.Cd;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.time.Clock;
import java.util.AbstractCollection.*;
import java.util.List;
import javax.ejb.EJB;
import model.CdFacade;
/**
*
* @author Mick
*/
@Named(value = "cdController")
@SessionScoped
public class CdController implements Serializable {
@EJB
private CdFacade cdFacade;
private Cd c = new Cd();
public Cd getC() {
return c;
}
public void setC(Cd c) {
this.c = c;
}
public String add()
{
this.cdFacade.create(this.c);
return "index";
}
public void delete(Cd c)
{
this.cdFacade.remove(c);
}
public CdController() {
}
public List<Cd> findAll()
{
return this.cdFacade.findAll();
}
}
好的,在我的add.xhtml(工作正常)我有这个
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Add CDs</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="Create/Edit"/></h1>
<h:panelGrid columns="2">
<h:outputLabel value="Id:" for="id" />
<h:inputText id="id" value="#{cdController.c.id}" title="Id" />
<h:outputLabel value="Artist:" for="artist" />
<h:inputText id="artist" value="#{cdController.c.artist}" title="Artist" />
<h:outputLabel value="Duration:" for="duration" />
<h:inputText id="duration" value="#{cdController.c.duration}" title="Duration" />
<h:outputLabel value="Title:" for="title" />
<h:inputText id="title" value="#{cdController.c.title}" title="Title" />
<h:commandButton value="save" action="#{cdController.add()}"></h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
Cd.java
package entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Mick
*/
@Entity
@Table(name = "CD")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Cd.findAll", query = "SELECT c FROM Cd c"),
@NamedQuery(name = "Cd.findById", query = "SELECT c FROM Cd c WHERE c.id = :id"),
@NamedQuery(name = "Cd.findByArtist", query = "SELECT c FROM Cd c WHERE c.artist = :artist"),
@NamedQuery(name = "Cd.findByDuration", query = "SELECT c FROM Cd c WHERE c.duration = :duration"),
@NamedQuery(name = "Cd.findByTitle", query = "SELECT c FROM Cd c WHERE c.title = :title")})
public class Cd implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Long id;
@Size(max = 255)
@Column(name = "ARTIST")
private String artist;
@Size(max = 255)
@Column(name = "DURATION")
private String duration;
@Size(max = 255)
@Column(name = "TITLE")
private String title;
public Cd() {
}
public Cd(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@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 Cd)) {
return false;
}
Cd other = (Cd) 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 "entity.Cd[ id=" + id + " ]";
}
}
CdFacade
package model;
import entity.Cd;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
*
* @author Mick
*/
@Stateless
public class CdFacade extends AbstractFacade<Cd> {
@PersistenceContext(unitName = "CD-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public CdFacade() {
super(Cd.class);
}
}
AbstractFacade
package model;
import java.util.List;
import javax.persistence.EntityManager;
/**
*
* @author Mick
*/
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}