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

时间:2013-04-16 11:36:00

标签: java-ee jpa ejbql

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

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

我不知道如何通过传递两个参数在我的代码中调用此查询:date,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 
}

1 个答案:

答案 0 :(得分:0)

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