gpt4 book ai didi

java - createSqlQuery Hibernate,Java

转载 作者:行者123 更新时间:2023-12-01 04:46:43 25 4
gpt4 key购买 nike

我需要在Java领域编写hibernate查询(createSQLquery)我有查询,但我有点不知道如何为我的查询创建条件。

基本上我必须将常规 where 子句转移到 hibernate .add(Restriction)

例如让我们采取:

where (name LIKE '%test%' or lastname LIKE '%test%') and (site_id = 2) and (date>=121212 and date <= 343343)

Query query = sess.createSQLQuery(sql).addScalar("ID", Hibernate.long);
query.add(Restrictions.or (
Restrictions.like("name", "%test%")
Restrictions.like("lastname", "Fritz%")
))
.add(Restrictions.eq("site_id", new Integer(2)))
.add(Restrictions.add(Restrictions.gr("date_start", 122122)
.add(Restrictions.le("date_end", 345433)));

我没有机会运行它,因为现在无法访问我的环境,只是想弄清楚这一点。

谢谢

最佳答案

你必须这样做

    Criteria criteria = session.createCriteria(YourPojo.class);
criteria.add(Restrictions.eq("id", id));
criteria .add(Restrictions.or (
Restrictions.like("name", "%test%"),
Restrictions.like("lastname", "Fritz%")));
criteria.add(Restrictions.eq("site_id", new Integer(2)))
criteria.add(Restrictions.add(Restrictions.gr("date_start", 122122)
.add(Restrictions.le("date_end", 345433)));
return (criteria.list());

关于java - createSqlQuery Hibernate,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com