向命名查询传递两个参数



我对我的实体"Intervention "有如下命名查询:

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

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

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

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

在这里遵循我的实体的代码干预:

    @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)。所以你的代码看起来像:

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

注意,当您使用Date时,您应该指定所需的TemporalType

相关内容

  • 没有找到相关文章

最新更新