如下是 HQL/QBC/Native SQL 三種查詢策略 sql
HQL 策略: 緩存
session.createQuery( "FROM Category c where c.name like 'Laptop%'" ); session
QBC 策略: 性能
session.createCriteria(Category . class ).add(Restrictions.like ( "name" , "Laptop%" )); spa
Native SQL 策略 orm
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%'" ); get
query.setMaxResults(10); it
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();}