使用日期列的 HQL 查询
我正在尝试创建一个查询,该查询将根据 createDate 字段是否位于传递的表示日期的字符串之后返回 Person 对象列表。
我的 createDate 字段在我的 Person 类中像这样映射
@Basic(optional = false)
@Column(name = "CREATE_DATE")
@Temporal(TemporalType.DATE)
private Date createDate;
。用于查找日期后所有人员结果的函数如下所示。
public List<Person> searchDate(String startDate) {
Session session = getSessionFactory().openSession();
String s_query = "FROM Person as p WHERE p.createDate >= :date";
Query query;
DateFormat df = new SimpleDateFormat('dd-MMM-yy');
try {
Date dt = df.parse(startDate);
query = session.createQuery(s_query).setDate("date", dt);
} catch(ParseException e){}
return (List<Person>)query.list();
}
I am trying to create a query that will return a List of Person objects based on if the createDate field falls after a passed string that represents a date.
My createDate fields is mapped like this in my Person class
@Basic(optional = false)
@Column(name = "CREATE_DATE")
@Temporal(TemporalType.DATE)
private Date createDate;
My function to find all person results after a date looks like this.
public List<Person> searchDate(String startDate) {
Session session = getSessionFactory().openSession();
String s_query = "FROM Person as p WHERE p.createDate >= :date";
Query query;
DateFormat df = new SimpleDateFormat('dd-MMM-yy');
try {
Date dt = df.parse(startDate);
query = session.createQuery(s_query).setDate("date", dt);
} catch(ParseException e){}
return (List<Person>)query.list();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为那里没有任何问题。该代码应该可以工作。您可以尝试将此作为替代方案。
我希望这会有所帮助。如果有什么问题可以在这里讨论,因为讨论是一种学习的方式。
I don't see any problem there. The code should work. You can try this as an alternative.
I hope this will help. If is there anything wrong discuss here because discussing is a way to learn.