我尝试使用EJB和JSF按名称设置搜索,但我有一个例外,每次都会持续存在而我找不到解决方案。
实体:
0 0 0 0
1 2 0.5 1.5
2 4 1.0 3.0
3 6 1.5 4.5
SearchService:
package com.mycompany.projetplanetart;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "etablissement")
@XmlRootElement
@NamedQueries({
@NamedQuery(name="Etablissement.findByName", query = "SELECT e FROM Etablissement e WHERE e.nom = :nom")
,@NamedQuery(name = "Etablissement.findAll", query = "SELECT e FROM Etablissement e")
, @NamedQuery(name = "Etablissement.findByNauto", query = "SELECT e FROM Etablissement e WHERE e.nauto = :nauto")
, @NamedQuery(name = "Etablissement.findByCodeINSEE", query = "SELECT e FROM Etablissement e WHERE e.codeINSEE = :codeINSEE")
, @NamedQuery(name = "Etablissement.findByIdEtab", query = "SELECT e FROM Etablissement e WHERE e.idEtab = :idEtab")})
public class Etablissement implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "N_auto")
private Integer nauto;
@Lob
@Size(max = 65535)
@Column(name = "nom")
private String nom;
@Lob
@Size(max = 65535)
@Column(name = "adresse")
private String adresse;
@Lob
@Size(max = 65535)
@Column(name = "complement")
private String complement;
@Column(name = "code_INSEE")
private Integer codeINSEE;
@Lob
@Size(max = 65535)
@Column(name = "commune")
private String commune;
@Lob
@Size(max = 65535)
@Column(name = "region_administrative")
private String regionAdministrative;
@Lob
@Size(max = 65535)
@Column(name = "geo_ban")
private String geoBan;
@Lob
@Size(max = 65535)
@Column(name = "geo_insee")
private String geoInsee;
@Lob
@Size(max = 65535)
@Column(name = "corrdonnees_finales")
private String corrdonneesFinales;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_etab")
private Integer idEtab;
public Etablissement() {
}
public Etablissement(Integer idEtab) {
this.idEtab = idEtab;
}
public Integer getNauto() {
return nauto;
}
public void setNauto(Integer nauto) {
this.nauto = nauto;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getComplement() {
return complement;
}
public void setComplement(String complement) {
this.complement = complement;
}
public Integer getCodeINSEE() {
return codeINSEE;
}
public void setCodeINSEE(Integer codeINSEE) {
this.codeINSEE = codeINSEE;
}
public String getCommune() {
return commune;
}
public void setCommune(String commune) {
this.commune = commune;
}
public String getRegionAdministrative() {
return regionAdministrative;
}
public void setRegionAdministrative(String regionAdministrative) {
this.regionAdministrative = regionAdministrative;
}
public String getGeoBan() {
return geoBan;
}
public void setGeoBan(String geoBan) {
this.geoBan = geoBan;
}
public String getGeoInsee() {
return geoInsee;
}
public void setGeoInsee(String geoInsee) {
this.geoInsee = geoInsee;
}
public String getCorrdonneesFinales() {
return corrdonneesFinales;
}
public void setCorrdonneesFinales(String corrdonneesFinales) {
this.corrdonneesFinales = corrdonneesFinales;
}
public Integer getIdEtab() {
return idEtab;
}
public void setIdEtab(Integer idEtab) {
this.idEtab = idEtab;
}
public Etablissement (Etablissement etab){
this.nom=etab.nom;
this.regionAdministrative=etab.regionAdministrative;
this.nauto=etab.nauto;
this.idEtab=etab.idEtab;
this.geoInsee=etab.geoInsee;
this.corrdonneesFinales=etab.corrdonneesFinales;
this.geoBan=etab.geoBan;
this.complement=etab.complement;
this.commune=etab.commune;
this.codeINSEE=etab.codeINSEE;
this.adresse=etab.adresse;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idEtab != null ? idEtab.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 Etablissement)) {
return false;
}
Etablissement other = (Etablissement) object;
if ((this.idEtab == null && other.idEtab != null) || (this.idEtab != null && !this.idEtab.equals(other.idEtab))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.projetplanetart.Etablissement[ idEtab=" + idEtab + " ]";
}
}
SearchBean:
import com.mycompany.projetplanetart.Etablissement;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class RechercherService {
@PersistenceContext
private EntityManager em;
public List<Etablissement> searchEtab(Integer id){
return em.createNamedQuery("Etablissement.findByIdEtab").setParameter("idEtab", id).getResultList();
}
}
resultat.xhtml:
import com.mycompany.projetplanetart.Etablissement;
import ejb.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@Named(value = "rechercheBean")
@RequestScoped
public class RechercheBean {
@Inject
RechercherService rechercherService;
private List<Etablissement> etab=new ArrayList<>();
public List<Etablissement> getEtab() {
return etab;
}
public void setEtab(List<Etablissement> etab) {
this.etab = etab;
}
private String nom;
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String search(){
rechercherService=new RechercherService();
this.etab=rechercherService.searchEtab(id);
return "resultat";
}
/**
* Creates a new instance of RechercheBean
*/
public RechercheBean() {
}
}
例外:
<?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> Facelet Title</title>
</h:head>
<h:body>
<f:view>
<h:form>
entré : <h:inputText value="#{rechercheBean.id}"/>
<h:commandButton value="Search" action="#{rechercheBean.search()}"/>
<h:form>
<h1><h:outputText value="List"/></h1>
<h:dataTable value="#{rechercheBean.etab}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Nauto"/>
</f:facet>
<h:outputText value="#{item.nauto}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Nom"/>
</f:facet>
<h:outputText value="#{item.nom}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Adresse"/>
</f:facet>
<h:outputText value="#{item.adresse}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Complement"/>
</f:facet>
<h:outputText value="#{item.complement}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="CodeINSEE"/>
</f:facet>
<h:outputText value="#{item.codeINSEE}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Commune"/>
</f:facet>
<h:outputText value="#{item.commune}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="RegionAdministrative"/>
</f:facet>
<h:outputText value="#{item.regionAdministrative}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="GeoBan"/>
</f:facet>
<h:outputText value="#{item.geoBan}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="GeoInsee"/>
</f:facet>
<h:outputText value="#{item.geoInsee}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="CorrdonneesFinales"/>
</f:facet>
<h:outputText value="#{item.corrdonneesFinales}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="IdEtab"/>
</f:facet>
<h:outputText value="#{item.idEtab}"/>
</h:column>
</h:dataTable>
</h:form>
</h:form>
</f:view>
</h:body>
谢谢。