简体   繁体   中英

Hibernate inject Criterion into Query

I have a code with Hibernate restrictions like:

Criterion budgetTypeRestriction;
budgetTypeRestriction = Restrictions.between("code", "01", "03");

And how I can inject Criterion into Query ?:

Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Regions");

It is easy for me collect Criterion logic, and then pass it to DAO, which is implemented throw Query and parameter binding.

In Query (HQL) you don't use Criterion.

Instead you use HQL expressions in where clause which are similar to SQL:

Query query = session.createQuery("from Regions r where r.code between '01 and '03'");

I hope you expect the below

List<Regions> regionsList=sessionfactory.getCurrentSession().createCriteria(Regions.class)
                          .add(Restrictions.between("code", "01", "03")).list();

You probably want to use the Criteria API instead of a query. You can add sql restrictions to a criteria with Restrictions.sqlRestriction() if you need that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM