小结Hibernate的查询方式
1. get() and load()
1. get() and load()
session.get(Clazz, id);
session.load(Clazz, id);
session.load(Clazz, id);
说明: load()与get()的区别
请注意如果没有匹配的数据库记录,load()方法可能抛出无法恢复的异常(unrecoverable exception)。如果类的映射使用了代理(proxy),load()方法会返回一个未初始化的代理,直到你调用该代理的某方法时才会去访问数据库。若你希望在某对象中创建一个指向另一个对象的关联,又不想在从数据库中装载该对象时同时装载相关联的那个对象,那么这种操作方式就用得上的了。如果为相应类映射关系设置了batch-size,那么使用这种操作方式允许多个对象被一批装载(因为返回的是代理,无需从数据库中抓取所有对象的数据)。
如果你不确定是否有匹配的行存在,应该使用get()方法,它会立刻访问数据库,如果没有对应的行,会返回null
2. HQL
//
返回一行记录
String hql = " from TOrder o where o.id = ? " ;
TOrder o = (TOrder) s.createQuery(hql)
.setParameter( 0 , orderId)
.uniqueResult();
// 命名参数
Query q = sess.createQuery( " from DomesticCat cat where cat.name = :name " );
q.setString( " name " , " Fritz " );
// 位置参数
Query q = sess.createQuery( " from DomesticCat cat where cat.name = ? " );
q.setString( 0 , " Izi " );
// 命名参数列表
Query q = sess.createQuery( " from DomesticCat cat where cat.name in (:namesList) " );
q.setParameterList( " namesList " , names);
// 分页查询
Query q = sess.createQuery( " from DomesticCat cat " );
q.setFirstResult( 20 );
q.setMaxResults( 10 );
List cats = q.list();
String hql = " from TOrder o where o.id = ? " ;
TOrder o = (TOrder) s.createQuery(hql)
.setParameter( 0 , orderId)
.uniqueResult();
// 命名参数
Query q = sess.createQuery( " from DomesticCat cat where cat.name = :name " );
q.setString( " name " , " Fritz " );
// 位置参数
Query q = sess.createQuery( " from DomesticCat cat where cat.name = ? " );
q.setString( 0 , " Izi " );
// 命名参数列表
Query q = sess.createQuery( " from DomesticCat cat where cat.name in (:namesList) " );
q.setParameterList( " namesList " , names);
// 分页查询
Query q = sess.createQuery( " from DomesticCat cat " );
q.setFirstResult( 20 );
q.setMaxResults( 10 );
List cats = q.list();
3. Criteria
List list
=
s.createCriteria(Enrolment.
class
)
.createAlias( " student " , " s " )
.createAlias( " course " , " c " )
.add( Restrictions.isNotEmpty( " s.enrolments " ) )
.setProjection( Projections.projectionList()
.add( Projections.property( " s.name " ) )
.add( Projections.property( " c.description " ) )
)
.setCacheable( true )
.list();
.createAlias( " student " , " s " )
.createAlias( " course " , " c " )
.add( Restrictions.isNotEmpty( " s.enrolments " ) )
.setProjection( Projections.projectionList()
.add( Projections.property( " s.name " ) )
.add( Projections.property( " c.description " ) )
)
.setCacheable( true )
.list();
4. Native SQL
String treeSql
=
""
+
" select {t.*}, level from tree t " +
" start with t.parent_id = 0 " +
" connect by prior t.id = t.parent_id " ;
List result = session.createSQLQuery(treeSql)
.addEntity( " t " , Tree. class )
.addScalar( " level " , Hibernate.INTEGER)
.list();
" select {t.*}, level from tree t " +
" start with t.parent_id = 0 " +
" connect by prior t.id = t.parent_id " ;
List result = session.createSQLQuery(treeSql)
.addEntity( " t " , Tree. class )
.addScalar( " level " , Hibernate.INTEGER)
.list();
5. Named SQL queries(不推荐)
6. filter(不推荐)
7. Detached queries(还没测试)
The DetachedCriteria class lets you create a query outside the scope of a session, and then later execute it using some arbitrary Session
参考
1. Hibernate Reference Documentation
2. Hibernate test case