4

There's a table name STUDENTS with column studentName and studentID

I want to run a query like

SELECT studentName 
FROM STUDENTS 
WHERE studentId ='1'; 

in hibernate criteria and want to save this result in some String variable

1 Answer 1

5

Something like:

String result = (String)sess.createCriteria(Students.class)
    .add(Restrictions.eq("studentId",1))
    .setProjection(Property.forName('studentName'))
    .uniqueResult();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks :) ,it works ,now this result is actually some date ,next i want to compare this date with some other date ,just check which date is greater like 2011-05-25 00:35:07 or 2011-07-25 00:35:08 in my previous SQL i was using (UNIX_TIMESTAMP(r.lastupdatedate) - UNIX_TIMESTAMP (e.lastlogindate))>=1 " , you have any idea what will i use nowi.e in hibernate criteria to compare two dates. thanks
You can compare this date using the Restrictions api. Something like: Restrictions.gt(yourDate, resultFromAboveDate). If you want to use the query above as a sub query in a larger query that compares dates then I recommend reading the hibernate documentation on detached queries with the Criteria api: docs.jboss.org/hibernate/core/3.3/reference/en/html/…
createCriteria is deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.