开发者

simplest criteria query

开发者 https://www.devze.com 2022-12-25 13:49 出处:网络
What is the simplest hibernate criteria query equivalent of SELECT name FROM people WHER开发者_JAVA百科E id=\'3\'

What is the simplest hibernate criteria query equivalent of

SELECT name FROM people WHER开发者_JAVA百科E id='3'

is it :

criteria.add(Expression.eq("id", 3));

and how can I retrieve it to String variable the value of name field, the IDs are unique


If you are querying by "id", why would you set up your Hibernate criteria to use "name"? If "id" is mapped as your primary key and you want to load the object directly, use the Get method off of Session.

Example:

People thePerson = (People) session.get(People.class, new Integer(1));

You might also want to try reading this.


I think you just want to project name and not get the full entity.

Criteria crit = session.createCriteria(People.class)
  .add(Restrictions.eq("id", 3);
ProjectionList projectList = Projections.projectionList();
projectList.add(Projections.property("name"));
crit.setProjection(projectList);

(String) crit.uniqueResult();

I would just go with using session.get(..) in this case as well since you are only retrieving 1 person and don't need to go through the trouble of specifying anything.

0

精彩评论

暂无评论...
验证码 换一张
取 消