将两个参数传递给命名查询

将两个参数传递给命名查询

本文介绍了将两个参数传递给命名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的实体干预有以下命名查询:

I have the following namedquery over my entity "Intervention ":

 @NamedQuery(name = "Intervention.findNextMission", query = " SELECT i FROM Intervention  i       WHERE i.heureDebut> :DateToBeSpecified and i.idAgent= :idAgent")`

我不知道如何通过传递两个参数在我的代码中调用此查询:date,id。通常它应该是这样的:

i dont know how to call this query in my code by passing two parameters : date,id. Normally it should be like this :

List <Intervention> ListOfInterventions = em.createNamedQuery("Intervention.findNextMission").setParameter().getResultList();

在setParameter()的括号之间放什么?

what to put between the parentheses of setParameter() ??

此处遵循我的实体代码干预:

here follow the code of my entity Intervention :

    @Entity
@Table(name = "intervention")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Intervention.findAll", query = "SELECT i FROM Intervention i"),
    @NamedQuery(name = "Intervention.findByIdIntervention", query = "SELECT i FROM Intervention i WHERE i.idIntervention = :idIntervention"),
    @NamedQuery(name = "Intervention.findByHeureDebut", query = "SELECT i FROM Intervention i WHERE i.heureDebut = :heureDebut"),
    @NamedQuery(name = "Intervention.findByHeureFin", query = "SELECT i FROM Intervention i WHERE i.heureFin = :heureFin"),
    @NamedQuery(name = "Intervention.findNextMission", query = " SELECT i FROM Intervention i WHERE i.heureDebut> :DateToBeSpecified and i.idAgent= :idAgent")})
public class Intervention implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "IdIntervention", nullable = false, length = 50)
    private String idIntervention;
    @Basic(optional = false)
    @NotNull
    @Column(name = "HeureDebut", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date heureDebut;
    @Basic(optional = false)
    @NotNull
    @Column(name = "HeureFin", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date heureFin;
    @Lob
    @Size(max = 65535)
    @Column(name = "NotesGeneral", length = 65535)
    private String notesGeneral;
    @JoinColumn(name = "IdValidation", referencedColumnName = "IdValidation", nullable = false)
    @ManyToOne(optional = false)
    private Tablevalidation idValidation;
    @JoinColumn(name = "IdNatureIntervention", referencedColumnName =  "IdNatureIntervention", nullable = false)
    @ManyToOne(optional = false)
    private Tablenatureintervention idNatureIntervention;
    @JoinColumn(name = "IdAgent", referencedColumnName = "IdAgent", nullable = false)
    @ManyToOne(optional = false)
    private Agent idAgent;
    @JoinColumn(name = "RefContrat", referencedColumnName = "RefContrat", nullable = false)
    @ManyToOne(optional = false)
    private Projet refContrat;

    public Intervention() {
    }
    //getters and setters
}


推荐答案

setParameter 接受(int position,Object value) (String parameterName,Object value)。因此,您的代码可能如下所示:

setParameter accepts either (int position,Object value) or (String parameterName,Object value). So your code could look as:

Date aDate=....
Long anId=....
List ListOfInterventions = em.createNamedQuery("Intervention.findNextMission")
  .setParameter("DateToBeSpecified",aDate,avax.persistence.TemporalType,DATE)
  .setParameter("idAgent",anId).getResultList();

请注意,当您使用日期时应指定 TemporalType

Note that when you use a Date you should specify the TemporalType intended.

这篇关于将两个参数传递给命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:00