1 命名参数
Bind parameters
Methods on Query are provided for binding values to named parameters or JDBC-style ?
parameters. Contrary to JDBC, Hibernate numbers parameters from zero. Named parameters
are identifiers of the form :name in the query string. The advantages of named parameters are
as follows:
• named parameters are insensitive to the order they occur in the query string
不受参数顺序的制约
• they can occur multiple times in the same query
在一个查询中可以出现多次
• they are self-documenting (是说可读性? 不是很理解)
//named parameter (preferred)
Query q = sess.createQuery("from DomesticCat cat where cat.name = :name");
q.setString("name", "Fritz");
Iterator cats = q.iterate();
//positional parameter
Query q = sess.createQuery("from DomesticCat cat where cat.name = ?");
q.setString(0, "Izi");
Iterator cats = q.iterate();
//named parameter list
List names = new ArrayList();
names.add("Izi");
names.add("Fritz");
Query q = sess.createQuery("from DomesticCat cat where cat.name in (:namesList)");
q.setParameterList("namesList", names);
List cats = q.list();
2 分页查询
不同数据库 的分页关键字是不同的,根据配置文件的方言设定值,hibernate自动生成分叶语句
这样就可以跨数据库。
Pagination
If you need to specify bounds upon your result set, that is, the maximum number of rows you want
to retrieve and/or the first row you want to retrieve, you can use methods of the Query interface:
Query q = sess.createQuery("from DomesticCat cat");
如果参数是0的话,那么是从第一条记录开始拿
q.setFirstResult(20);
q.setMaxResults(10);
List cats = q.list();
Hibernate knows how to translate this limit query into the native SQL of your DBMS.