我正在使用 Derby 数据库,这是执行查找的实体
package ca.etsmtl.gti525.entity.presentation;
import ca.etsmtl.gti525.commun.CommunService;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Remarque : peu importe la date et l'heure une représantation ce fait que dans une seul salle a la fois. (pour un même spectacle)
* @author REDOUANE
*
*/
@Entity
@Table(name = "SHOW_REPRESENTATION")
public class Representation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "DATE_DEBUT")
@Temporal(TemporalType.DATE)
private Date dateDebut;
// @Column(name = "HEURE_DEBUT")
// @Temporal(TemporalType.TIME)
// private Calendar heureDebut;
@Column(name = "NB_BILLETS_DISPO")
private Integer nbBilletsDispo;
@Column(name = "QTE_SELECTED")
private Integer QTE=0;
@Column(name = "PRIX")
private Float prix;
@OneToOne
private Salle salle;
@ManyToOne
private Spectacle spectacle;
@OneToOne(mappedBy = "representation")
private Billet billet;
public Integer getQTE() {
return QTE;
}
public void setQTE(Integer QTE) {
this.QTE = QTE;
}
public Billet getBillet() {
return billet;
}
public void setBillet(Billet billet) {
this.billet = billet;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Spectacle getSpectacle() {
return spectacle;
}
public void setSpectacle(Spectacle spectacle) {
this.spectacle = spectacle;
}
public Integer getNbBilletsDispo() {
return nbBilletsDispo;
}
public void setNbBilletsDispo(Integer nbBilletsDispo) {
this.nbBilletsDispo = nbBilletsDispo;
//int min_ticket_allowed = nbBilletsDispo;
// if (nbBilletsDispo >= 6) min_ticket_allowed = 6;
// optionsQte2.clear();
// for (int i=0; i< min_ticket_allowed +1; i++) optionsQte2.add(i);
}
public Float getPrix() {
return prix;
}
public void setPrix(Float prix) {
this.prix = prix;
}
public Date getDateDebut() {
return dateDebut;
}
public void setDateDebut(Date dateDebut) {
this.dateDebut = dateDebut;
}
public Salle getSalle() {
return salle;
}
public void setSalle(Salle salle) {
this.salle = salle;
}
public void verfierDispo(){
// CommunService.addWarn("QTE !", ""+this.QTE);
if(this.nbBilletsDispo<this.QTE){
CommunService.addWarn("ATTENTION !", "Le nombre de billets disponible est insuffisant");
}
}
}
这是我的 dao 类,其中包含 find 方法
package ca.etsmtl.gti525.dao.presentation;
import ca.etsmtl.gti525.entity.presentation.Artiste;
import ca.etsmtl.gti525.entity.presentation.Billet;
import ca.etsmtl.gti525.entity.presentation.Representation;
import ca.etsmtl.gti525.entity.presentation.Salle;
import ca.etsmtl.gti525.entity.presentation.Spectacle;
import ca.etsmtl.gti525.entity.vente.Reservation;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class DaoJpaPresentation implements IDaoPresentation, Serializable {
private static final long serialVersionUID = 1L;
//l'ingection du "e-ticket-jpa-hibernate-PU"
@PersistenceContext
private EntityManager em;
// liste des clients
@Override
public List<Artiste> getAllArtistes() {
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Artiste.class));
return em.createQuery(cq).getResultList();
} catch (Exception ex) {
//getLogger().log(Level.WARNING, "Exception lors de l''appel au service " + entityClass + " Impl.findAll {0} : ", ex.getMessage());
return null;
}
}
@Override
public List<Spectacle> getAllSpectacle() {
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Spectacle.class));
return em.createQuery(cq).getResultList();
} catch (Exception ex) {
//getLogger().log(Level.WARNING, "Exception lors de l''appel au service " + entityClass + " Impl.findAll {0} : ", ex.getMessage());
return null;
}
}
@Override
public List<Representation> getAllRepresentation() {
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Representation.class));
return em.createQuery(cq).getResultList();
} catch (Exception ex) {
//getLogger().log(Level.WARNING, "Exception lors de l''appel au service " + entityClass + " Impl.findAll {0} : ", ex.getMessage());
return null;
}
}
@Override
public void createBillet(Billet billet) {
try {
em.persist(billet);
} catch (Exception ex) {
Logger.getLogger(DaoJpaPresentation.class.toString()).log(Level.SEVERE, ex.toString());
}
}
@Override
public Representation findRepresentation(Long id) {
try {
Query query;
query = (Query) em.createQuery("SELECT a FROM Representation a where a.id=:id");
query.setParameter("id", id);
return ((Representation) query.getSingleResult());
} catch (Exception ex) {
Logger.getLogger(DaoJpaPresentation.class.toString()).log(Level.SEVERE, ex.toString());
return null;
}
}
@Override
public Salle findSalle(Long id) {
return null;
}
@Override
public Reservation findReservation(Long id) {
return null;
}
@Override
public void createRepresentation(Representation representation) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void createSalle(Salle salle) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void createSpectacle(Spectacle spectacle) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void createArtist(Artiste artist) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Representation findRepresentationByID(Long id) {
try {
return em.find(Representation.class, id);
} catch (Exception ex) {
//getLogger().log(Level.WARNING, "Exception lors de l''appel au service " + entityClass + " Impl.find {0} : ", ex.getMessage());
System.out.print("Erreure ici au DAO");
return null;
}
}
}
这就是我如何调用该方法
Long idRep = panier.getIdRep();
bil.setRepresentation(this.daoPresentation.findRepresentationByID(idRep));
所以最后一行给了我一个空指针异常,因为 find 方法没有返回表示对象
java.lang.NullPointerException
at ca.etsmtl.gti525.vente.ProcessPaiementControleur.save(ProcessPaiementControleur.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我正在搜索,但当我执行这个 sql 查询时,它给了我正确的行
select * from APP.SHOW_REPRESENTATION a where a.id=4