开发者

Executing a query --- Hibernate + Spring

开发者 https://www.devze.com 2023-02-03 08:19 出处:网络
I am using spring and hibernate together. Can any body suggest me how can i execute a simple query? For example, I want to execute \"select count(*) from USER_DETA开发者_如何学CILS\";

I am using spring and hibernate together.

Can any body suggest me how can i execute a simple query?

For example, I want to execute "select count(*) from USER_DETA开发者_如何学CILS";

Thanks,

Narendra


Hiii... Use criteria and projection together. Projection

Criteria crit = session.createCriteria(USER_DETAILS.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.countDistinct("Id"));
crit.setProjection(projList);

crit.list will give you count. This is simple hibernate code you can figure out spring + hibernat with this example.


Hibernate is a Object-Relational Mapping tool. You first map a User object to USER_DETAILS table and then you write HQL (Hibernate Query Language) against the mapped User object (not USER_DETAILS table). For e.g you can write the query you have posted as below using HQL.

select count(user) from User user;

0

精彩评论

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