swateek
swateek

Reputation: 7590

Error getting count of unique results in hibernate

For defining criteria, I am using the below code piece

Conjunction c1 = Restrictions.conjunction();
c1.add(Restrictions.eq("owner", 123));
c1.add(Restrictions.eq("shared", 'N'));
c1.add(Restrictions.eq("cId", 20));

Conjunction c2 = Restrictions.conjunction();
c2.add(Restrictions.eq("shared", 'Y'));
c2.add(Restrictions.eq("cId", 20));

criteria.add(Restrictions.or(c1, c2));

Then, I want to fetch the unique results for this criteria.

CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
Projection originalProjection = criteriaImpl.getProjection();
ResultTransformer originalResultTransformer = criteriaImpl.getResultTransformer();
Object rowCount = criteria.setProjection(Projections.rowCount()).uniqueResult();

int rc = (Integer) rowCount;

However, the code fails at criteria.setProjection(Projections.rowCount()).uniqueResult() with an error printed on server logs like this below:

Info: forcing batcher resource cleanup on transaction completion; forgot to close ScrollableResults/Iterator?

And the exception that comes along is:

java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String

Any idea where I am going wrong?

Upvotes: 2

Views: 462

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24433

It's probably this

c1.add(Restrictions.eq("shared", 'N'));

Try with this, in both places where you use it (" instead of ')

c1.add(Restrictions.eq("shared", "N"));

Upvotes: 3

Related Questions