以下是 HQL/QBC/Native SQL 三种查询策略
HQL 策略:
session.createQuery( "FROM Category c where c.name like 'Laptop%'" );
QBC 策略:
session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "Laptop%" ));
Native SQL 策略
session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like 'Laptop%'" ).addEntity( "c" ,Category . class );
String hql = "SELECT user.username,user.password FROM UserInfo user WHERE user.username = (SELECT info.username FROM UserInfo info)" ;
Query query = session.createQuery( "FROM Category c where c.name like 'Laptop%'" );
query.setMaxResults(10);
Query query = session.createQuery( "FROM Category c where c.name like :name" );
query.setParameter( "name" , "top" );
Query query = session.createQuery( "FROM Category c where c.name like ?" );
query.setParameter( 0 , "top" );
query.setTimeout(60); // 查询超时
query.setFetchSize(50); // 批量抓取
List list = query.list();
生成 sql:select category_id,name,parent_id from category where name like ?
Iterator categories = query.iterate();
生成 sql:select category_id,from category where name like ?
使用 iterate() 查询时将对象加载到高速缓存中,所以效率较 list() 要高 , 但提升性能不大。
在 Hibernate 查询时可能存在结果集太大无法载入内存的问题,可以阐释使用可滚动结果集 scrollable result 这种方法获取所需要的数据。
ScrollableResults cusor = session.createQuery( "FROM Category c" ).scroll();
while (cusor.next()){cusor.get();}
以下是 HQL/QBC/Native SQL 三种查询策略
HQL 策略:
session.createQuery( "FROM Category c where c.name like 'Laptop%'" );
QBC 策略:
session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "Laptop%" ));
Native SQL 策略
session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like 'Laptop%'" ).addEntity( "c" ,Category . class );
Criteria criteria = session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "Laptop%" ));
criteria.addOrder(Order.asc ( "name" ));
criteria.setFirstResult(0); // 初始行数
criteria.setMaxResults(20); // 每页显示行数
Criteria criteria = session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "top%" ));
criteria.setTimeout(60);
criteria.setFetchSize(50);
List list = criteria .list();
生成 sql:select category_id,name,parent_id from category where name like ?
Iterator categories = criteria .iterate();
生成 sql:select category_id,from category where name like ?
使用 iterate() 查询时将对象加载到高速缓存中,所以效率较 list() 要高 , 但提升性能不大。
在 Hibernate 查询时可能存在结果集太大无法载入内存的问题,可以阐释使用可滚动结果集 scrollable result 这种方法获取所需要的数据。
ScrollableResults cusor = criteria.scroll();
while (cusor.next()){cusor.get();}
以下是 HQL/QBC/Native SQL 三种查询策略
HQL 策略:
session.createQuery( "FROM Category c where c.name like 'Laptop%'" );
QBC 策略:
session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "Laptop%" ));
Native SQL 策略
session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like 'Laptop%'" ).addEntity( "c" ,Category . class );
Query sqlQuery = session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like 'Laptop%'" ).addEntity( "c" ,Category . class );
sqlQuery.setFirstResult(0); // 初始行数
sqlQuery.setMaxResults(20); // 每页显示行数
Query sqlQuery = session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like :name" ).addEntity( "c" ,Category . class );
sqlQuery.setParameter( "name" , "top" );
Query sqlQuery = session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like ?" ).addEntity( "c" ,Category . class );
query.setParameter( 0 , "top" );
sqlQuery.setTimeout(60); // 查询超时
sqlQuery.setFetchSize(50); // 批量抓取
List list = sqlQuery .list();
生成 sql:select category_id,name,parent_id from category where name like ?
Iterator categories = sqlQuery .iterate();
生成 sql:select category_id,from category where name like ?
使用 iterate() 查询时将对象加载到高速缓存中,所以效率较 list() 要高 , 但提升性能不大。
在 Hibernate 查询时可能存在结果集太大无法载入内存的问题,可以阐释使用可滚动结果集 scrollable result 这种方法获取所需要的数据。
ScrollableResults cusor = session.createQuery( "FROM Category c" ).scroll();
while (cusor.next()){cusor.get();}