它说我不能使用这类变量的运算符,但为什么呢?
我的想法是有两个文本字段,一个用于接收结束日期,另一个用于开始日期,作为参数值传递,然后返回具有亲属日期的对象列表
[59, 66] The identification variable 'endDate' cannot be used in conjunction `with the <= operator.
[35, 44] The identification variable 'startDate' is not defined in the FROM clause.
[59, 66] The identification variable 'endDate' is not defined in the FROM clause.
即时通讯使用jDatachosser,以及我如何获取日期
String startDate= ((JTextField) txtDatainicio.getDateEditor().getUiComponent()).getText();
String endDate= ((JTextField) txtDataFim.getDateEditor().getUiComponent()).getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
那就是返回列表的方法
public List<Ordemservico> findDates(String date1, String date2) {
EntityManager em = getEntityManager();
em.getTransaction().begin();
TypedQuery<Ordemservico> query = em.createQuery("select u from Ordemservico u where startDate>= :date1 AND endDate <= :date2", Ordemservico.class);
query.setParameter("date1", date1);
query.setParameter("date2", date2);
List<Ordemservico> layoutsSelected = query.getResultList();
em.getTransaction().commit();
return layoutsSelected;
}
我的classe
public class Ordemservico implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idordemservico")
private Integer idordemservico;
@Column(name = "identificadoros")
private Integer identificadoros;
@Column(name = "mes")
private Integer mes;
@Column(name = "ano")
private Integer ano;
@Column(name = "assunto")
private String assunto;
@Basic(optional = false)
@Column(name = "publicitario")
private String publicitario;
@Basic(optional = false)
@Column(name = "layoutos")
private String layoutos;
@Basic(optional = false)
@Column(name = "startDate")
@Temporal(TemporalType.DATE)
private Date startDate;
@Column(name = "endDate")
@Temporal(TemporalType.DATE)
private Date endDate;
@Column(name = "dataevento")
答案 0 :(得分:1)
在JPA中,当你创建查询时,你必须把对象放到属性所属的位置,试试:
TypedQuery<Ordemservico> query = em.createQuery("select u from Ordemservico u where u.startDate>= :date1 AND u.endDate <= :date2", Ordemservico.class);
查看条件 u。 startDate&gt; =:date1而不仅仅是startDate&gt; =:date1
答案 1 :(得分:0)
@Column(name = "startDate")
@Temporal(TemporalType.DATE)
private Date startDate;
@Column(name = "endDate")
@Temporal(TemporalType.DATE)
private Date endDate;
但是您要将查询中的日期参数设置为字符串。相反,他们必须是日期类型。
findDates(String date1, String date2)
...
query.setParameter("date1", date1);//value should be of Date type
query.setParameter("date2", date2);//same as above
更改后,它应如下所示:
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public List<Ordemservico> findDates(String date1, String date2) {
Date parsedDate1 = sdf.parse(date1);
Date parsedDate2 = sdf.parse(date2);
EntityManager em = getEntityManager();
em.getTransaction().begin();
TypedQuery<Ordemservico> query = em.createQuery("select u from Ordemservico u where u.startDate>= :date1 AND u.endDate <= :date2", Ordemservico.class)
query.setParameter("date1", parsedDate1);// value must be date as the property we are comparing is of Date type
query.setParameter("date2", parsedDate2);// same as above
List<Ordemservico> layoutsSelected = query.getResultList();
em.getTransaction().commit();
return layoutsSelected;
}
此外,如果您只想在日期级别比较忽略时间值,您可以查看以下方法:
//Class: TypedQuery
setParameter(String name, Date value,TemporalType temporalType);