Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Criteria query statement

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Criteria query sentence example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Criteria query

Package com.shiryu.otm; import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Expression; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Subqueries Public class Test2 {public static void main (String args []) {SessionFactory factory = new Configuration (). Configure (). BuildSessionFactory (); Session session = factory.openSession (); Transaction tr = session.beginTransaction (); / / Criteria query statement supplement / / one, compound query / / in this one-to-many example. We use a composite query to query all users and their addresses. / / Criteria criteria = session.createCriteria (User.class); / / List list = criteria.list (); / for (int I = 0; I

< list.size(); i++) { // User user = (User) list.get(i); // System.out.println("\t user:"+i+" name is:" + user.getName()); // // Set addrSet = user.getAddresses(); // Iterator it = addrSet.iterator(); // while(it.hasNext()){ // Address addr = (Address)it.next(); // System.out.println("\t\t user:"+i+" address is:"+addr.getAddress()); // } // } // 通过Criteria查询条件,我们可以查询位于上海的用户 // Criteria criteria = session.createCriteria(User.class); // // Criteria addCriteria = // criteria.createCriteria("addresses");//这里addresses和User类对应 // addCriteria.add(Expression.like("address", // "%shanghai%"));//这里构造新的Criteria查询过滤条件 // List list = criteria.list(); // // for (int i = 0; i < list.size(); i++) { // User user = (User) list.get(i); // System.out.println("\t user:" + i + " name is:" + user.getName()); // // Set addrSet = user.getAddresses(); // Iterator it = addrSet.iterator(); // while (it.hasNext()) { // Address addr = (Address) it.next(); // System.out.println("\t\t user:" + i + " address is:" // + addr.getAddress()); // } // } /* * 二、DetachedCriteria的使用 hibernate2 中critria * 生命周期位于宿主Session生命周期之内,session创建criteria实例,Session被销毁,critria实例也随之失效 * 这很大程度上限制了criteria的重用hibernate3 中提供了DetachedCriteria * 它可以脱离Session实例独立存在,我们可以把通用的criteria查询条件进行抽离,更好的实现代码重用 */ DetachedCriteria deCriteria = DetachedCriteria.forClass(User.class); deCriteria.add(Expression.eq("name", "zhaiyu")); deCriteria.add(Expression.eq("age", new Integer(21))); Criteria criteria = deCriteria.getExecutableCriteria(session);// 在其需要的时候与session进行绑定 Iterator it = criteria.list().iterator(); while (it.hasNext()) { User user = (User) it.next(); System.out.println(user.getName()); } /* * DetachedCriteria 也可以用于子查询表达 下例返回了年龄超过了平均值的用户记录 */ DetachedCriteria avgAge = DetachedCriteria.forClass(User.class); avgAge.setProjection(Projections.avg("age")); Criteria criteria2 = session.createCriteria(User.class); criteria2.add(Subqueries.propertyGt("age", avgAge)); List list = criteria2.list(); // 我们可以将DetachedCriteria纳入查询表达式 :select ... from User where age >

(select / / avg (age) from User) / / (select avg (age) from User) described by DetachedCriteria avgAge / * III. Advanced features of Criteria query statements * / / limit the range of returned records Criteria criteria3 = session.createCriteria (User.class); / / limit 20 records criteria3.setFirstResult (100) starting from Article 100 Criteria3.setMaxResults (20); / / sort records Criteria criteria4 = session.createCriteria (User.class); criteria4.add (Expression.eq ("id", new Integer (2)); criteria4.addOrder (Order.asc ("name")); criteria4.addOrder (Order.desc ("id")) / * Criteria grouping and statistics * / Criteria criteria5 = session.createCriteria (User.class); criteria5.setProjection (Projections.groupProperty ("age")); Iterator it3 = criteria5.list () .iterator (); while (it.hasNext ()) {System.out.println (it.next ()) } Thank you for reading this article carefully. I hope the article "sample Analysis of Criteria query sentences" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report