Search code examples
javasqlhibernatecriteria

Create a Hibernate Criteria sentence from sql


How can I create a Hibernate Criteria from this SQL query???

select di.*
from device_information di,
category_product cp
where di.ID_CATEGORY_PRODUCT=cp.ID
and cp.ID_INTERNAL_GROUP= 345

Actually I have this code line:

Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(DeviceInformation.class); criteria.createCriteria("categoryProduct"); criteria.add(Restrictions.eq("internalGroup",internalGroupSelected)); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

List list=criteria.list();

But I have this error: could not resolve property: internalGroup of: com.as5fx.business.model.DeviceInformation


Solution

  • you have the internalGroup property in your category_product, so you cannot find that when you are querying on DeviceInformation. Try like below if you have a relationship between DeviceInformation and categoryProduct:

    List list = getSessionFactory().getCurrentSession().createCriteria(DeviceInformation.class,"deviceInformation")
        .createAlias("deviceInformation.categoryProduct","product")
        .add(Restrictions.eq("product.internalGroup",internalGroupSelected))
        .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();