Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria Query Issue with Projection and restriction

I am trying to get selected columns from a table using hibernate criteria query

Criteria cr = session.createCriteria(OfferCashbackMaster.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("txnType"), "txnType")
      .add(Projections.property("off_Discription"), "off_Discription"))
    .setResultTransformer(Transformers.aliasToBean(OfferCashbackMaster.class))
    .add(Restrictions.and(Restrictions.eq("aggregatorId", aggregatorId),
                           Restrictions.eq("txnType", txnType)));

The name txnType mentioned in projection is having a clash with restriction.

Giving me the following error

Hibernate: 
select 
    this_.OFFER_CODE as y0_, 
    this_.TXN_TYPE as y1_, 
    this_.VALID_TO as y2_, 
    this_.OFFER_DISCRIPTION as y3_ 
    from OFFER_CASHBACK_MASTER this_ 
where 
    (this_.AGGREGATOR_ID=? and y1_=?)

2018-02-25/15:42:41.756  WARN: util.JDBCExceptionReporter - 
SQL Error: 1054, SQLState: 42S22
2018-02-25/15:42:41.757 ERROR: util.JDBCExceptionReporter - 
Unknown column 'y1_' in 'where clause'

How can we solve this issue?

like image 401
Abhishek Patil Avatar asked Feb 25 '18 10:02

Abhishek Patil


People also ask

How do you put restrictions in criteria query?

Restrictions with CriteriaCriteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.

What is restriction in Hibernate criteria?

1.2. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method. This method takes an org. hibernate.

Is Hibernate criteria deprecated?

Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API.


2 Answers

If you are using Hibernate 3.2.6, 3.0.5, 3.5.5, 3.6.0.Beta3 then it's time for migrating to newer version of Hibernate.

Reported bug: Criteria Query Issue with Projection and restriction

Link: https://hibernate.atlassian.net/browse/HHH-3371

Affects Version/s: 3.2.6 3.0.5, 3.5.5, 3.6.0.Beta3

Component/s: query-criteria

Fix Version: 3.6.0.Beta4 and above.

like image 145
Shubham Kadlag Avatar answered Sep 18 '22 16:09

Shubham Kadlag


I believe you're using the outdated version of hibernate.

The issue is addressed and resolved for a long time:

https://hibernate.atlassian.net/browse/HHH-817

Fixed version: 3.6.0.Beta4

like image 42
Mạnh Quyết Nguyễn Avatar answered Sep 19 '22 16:09

Mạnh Quyết Nguyễn