HQL查询方法一般使用的比较多,一般是通过query方式来查询的.例子如下:
/**
* 根据名字来查询
*
* @param name
*/
static void query(String name) {
Session s = null;
try{
s = HibernateUtil.getSession();
// String hql = "from User as user where user.name=?";
String hql = "from User as user where user.name=:name";//采用这种方式的时候,就用setString(String,String)
Query query = s.createQuery(hql);
query.setString("name", name);
List list = query.list();
//查询从某一条开始到某一条的分页
query.setFirstResult(10);
query.setMaxResults(20);
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
}finally{
if(s!=null){
s.close();
}
}
}
而Criteria是一种比HQL更面向对象的查询方式:
创建方式如下:Criteria crit = session.createCriteria(Object.class);
crit.add(Restrictions.eqProperty(a,b));
eg:
static void query(String name) {
Session s = null;
try{
s = HibernateUtil.getSession();
Criteria c = s.createCriteria(User.class);
c.add(Restrictions.eq("name", name));
c.add(Restrictions.gt("birthday", new Date()));
List list = c.list();
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
}finally{
if(s!=null){
s.close();
}
}
}
关于Restrictions的API如下
public class Restrictions {
Restrictions() {
//cannot be instantiated
}
/**
* Apply an "equal" constraint to the identifier property
* @param propertyName
* @param value
* @return Criterion
*/
public static Criterion idEq(Object value) {
return new IdentifierEqExpression(value);
}
/**
* Apply an "equal" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression eq(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, "=");
}
/**
* Apply a "not equal" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression ne(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, "<>");
}
/**
* Apply a "like" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression like(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, " like ");
}
/**
* Apply a "like" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression like(String propertyName, String value, MatchMode matchMode) {
return new SimpleExpression(propertyName, matchMode.toMatchString(value), " like " );
}
/**
* A case-insensitive "like", similar to Postgres <tt>ilike</tt>
* operator
*
* @param propertyName
* @param value
* @return Criterion
*/
public static Criterion ilike(String propertyName, String value, MatchMode matchMode) {
return new IlikeExpression(propertyName, value, matchMode);
}
/**
* A case-insensitive "like", similar to Postgres <tt>ilike</tt>
* operator
*
* @param propertyName
* @param value
* @return Criterion
*/
public static Criterion ilike(String propertyName, Object value) {
return new IlikeExpression(propertyName, value);
}
/**
* Apply a "greater than" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression gt(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, ">");
}
/**
* Apply a "less than" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression lt(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, "<");
}
/**
* Apply a "less than or equal" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression le(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, "<=");
}
/**
* Apply a "greater than or equal" constraint to the named property
* @param propertyName
* @param value
* @return Criterion
*/
public static SimpleExpression ge(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, ">=");
}
/**
* Apply a "between" constraint to the named property
* @param propertyName
* @param lo value
* @param hi value
* @return Criterion
*/
public static Criterion between(String propertyName, Object lo, Object hi) {
return new BetweenExpression(propertyName, lo, hi);
}
/**
* Apply an "in" constraint to the named property
* @param propertyName
* @param values
* @return Criterion
*/
public static Criterion in(String propertyName, Object[] values) {
return new InExpression(propertyName, values);
}
/**
* Apply an "in" constraint to the named property
* @param propertyName
* @param values
* @return Criterion
*/
public static Criterion in(String propertyName, Collection values) {
return new InExpression( propertyName, values.toArray() );
}
/**
* Apply an "is null" constraint to the named property
* @return Criterion
*/
public static Criterion isNull(String propertyName) {
return new NullExpression(propertyName);
}
/**
* Apply an "equal" constraint to two properties
*/
public static PropertyExpression eqProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, "=");
}
/**
* Apply a "not equal" constraint to two properties
*/
public static PropertyExpression neProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, "<>");
}
/**
* Apply a "less than" constraint to two properties
*/
public static PropertyExpression ltProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, "<");
}
/**
* Apply a "less than or equal" constraint to two properties
*/
public static PropertyExpression leProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, "<=");
}
/**
* Apply a "greater than" constraint to two properties
*/
public static PropertyExpression gtProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, ">");
}
/**
* Apply a "greater than or equal" constraint to two properties
*/
public static PropertyExpression geProperty(String propertyName, String otherPropertyName) {
return new PropertyExpression(propertyName, otherPropertyName, ">=");
}
/**
* Apply an "is not null" constraint to the named property
* @return Criterion
*/
public static Criterion isNotNull(String propertyName) {
return new NotNullExpression(propertyName);
}
/**
* Return the conjuction of two expressions
*
* @param lhs
* @param rhs
* @return Criterion
*/
public static LogicalExpression and(Criterion lhs, Criterion rhs) {
return new LogicalExpression(lhs, rhs, "and");
}
/**
* Return the disjuction of two expressions
*
* @param lhs
* @param rhs
* @return Criterion
*/
public static LogicalExpression or(Criterion lhs, Criterion rhs) {
return new LogicalExpression(lhs, rhs, "or");
}
/**
* Return the negation of an expression
*
* @param expression
* @return Criterion
*/
public static Criterion not(Criterion expression) {
return new NotExpression(expression);
}
/**
* Apply a constraint expressed in SQL, with the given JDBC
* parameters. Any occurrences of <tt>{alias}</tt> will be
* replaced by the table alias.
*
* @param sql
* @param values
* @param types
* @return Criterion
*/
public static Criterion sqlRestriction(String sql, Object[] values, Type[] types) {
return new SQLCriterion(sql, values, types);
}
/**
* Apply a constraint expressed in SQL, with the given JDBC
* parameter. Any occurrences of <tt>{alias}</tt> will be replaced
* by the table alias.
*
* @param sql
* @param value
* @param type
* @return Criterion
*/
public static Criterion sqlRestriction(String sql, Object value, Type type) {
return new SQLCriterion(sql, new Object[] { value }, new Type[] { type } );
}
/**
* Apply a constraint expressed in SQL. Any occurrences of <tt>{alias}</tt>
* will be replaced by the table alias.
*
* @param sql
* @return Criterion
*/
public static Criterion sqlRestriction(String sql) {
return new SQLCriterion(sql, ArrayHelper.EMPTY_OBJECT_ARRAY, ArrayHelper.EMPTY_TYPE_ARRAY);
}
/**
* Group expressions together in a single conjunction (A and B and C...)
*
* @return Conjunction
*/
public static Conjunction conjunction() {
return new Conjunction();
}
/**
* Group expressions together in a single disjunction (A or B or C...)
*
* @return Conjunction
*/
public static Disjunction disjunction() {
return new Disjunction();
}
/**
* Apply an "equals" constraint to each property in the
* key set of a <tt>Map</tt>
*
* @param propertyNameValues a map from property names to values
* @return Criterion
*/
public static Criterion allEq(Map propertyNameValues) {
Conjunction conj = conjunction();
Iterator iter = propertyNameValues.entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry me = (Map.Entry) iter.next();
conj.add( eq( (String) me.getKey(), me.getValue() ) );
}
return conj;
}
/**
* Constrain a collection valued property to be empty
*/
public static Criterion isEmpty(String propertyName) {
return new EmptyExpression(propertyName);
}
/**
* Constrain a collection valued property to be non-empty
*/
public static Criterion isNotEmpty(String propertyName) {
return new NotEmptyExpression(propertyName);
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeEq(String propertyName, int size) {
return new SizeExpression(propertyName, size, "=");
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeNe(String propertyName, int size) {
return new SizeExpression(propertyName, size, "<>");
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeGt(String propertyName, int size) {
return new SizeExpression(propertyName, size, "<");
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeLt(String propertyName, int size) {
return new SizeExpression(propertyName, size, ">");
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeGe(String propertyName, int size) {
return new SizeExpression(propertyName, size, "<=");
}
/**
* Constrain a collection valued property by size
*/
public static Criterion sizeLe(String propertyName, int size) {
return new SizeExpression(propertyName, size, ">=");
}
public static NaturalIdentifier naturalId() {
return new NaturalIdentifier();
}
}
除了构造函数,全都是静态的.
分享到:
相关推荐
本篇文章将深入探讨Hibernate中的三种主要查询方式:HQL(Hibernate Query Language)、Criteria API以及原生SQL。 一、HQL(Hibernate Query Language) HQL是Hibernate提供的一种面向对象的查询语言,它类似于SQL...
本资料主要探讨的是Hibernate中的两种查询方式:HQL(Hibernate Query Language)和Criteria API。 HQL,全称为Hibernate查询语言,是Hibernate提供的一种面向对象的查询语言,类似于SQL,但更加面向对象。HQL直接...
在Java世界中,Hibernate是一个非常流行的...同时,Hibernate还提供了Criteria API和Querydsl等其他查询方式,提供了更多灵活性和选择。通过深入学习和实践,可以更好地利用Hibernate优化数据库交互,提高开发效率。
【描述】:在ORM框架Hibernate中,数据查询和检索是一个核心功能,它提供了多种查询方式,包括标准化对象查询(Criteria Query)、Hibernate查询语言(HQL)和原生SQL查询。其中,HQL作为官方推荐的查询方式,具有...
本文旨在为读者呈现不同的方式查询方式:HQL方式的查询、SQL原生态SQL方式的查询、以及Criteria方式的查询,拓展查询的不同实现思路,开阔视野、并涵盖了部分的Hibernate增删改的基本操作。
HQL和Criteria API提供了更面向对象的查询方式,而JPA注解则简化了对象和数据库表之间的映射。不论选择哪种方式,了解和掌握这些方法都将有助于提高开发效率并减少出错的可能性。 在提供的压缩包文件“hibernate_...
本教程将深入探讨"day36-hibernate检索和优化 02-Hibernate检索方式:简单查询及别名查询"的主题,通过源码分析和实际工具的应用,来提升数据库查询的效率。 首先,我们了解Hibernate的检索方式。在Hibernate中,...
- **查询优化**:合理地设计表结构和索引,使用合适的查询语句,避免全表扫描等方式来提高查询效率。 #### 四、总结 通过上述介绍,我们可以了解到 HQL 在 Hibernate 中的重要性和实用性。它不仅提供了简洁易用的...
除了HQL,Hibernate还提供了Criteria API,它提供了一种类型安全的、面向对象的查询方式。Criteria查询更动态,适合在编程时构建查询条件,而HQL更适合静态的、预先定义的查询。两者各有优势,可以根据项目需求和...
除了HQL之外,Hibernate还提供了Criteria API来进行查询。Criteria API是一种类型安全的API,可以提供更灵活的查询方式。 ```java Session session = HibernateUtil.getSession(); Criteria criteria = session....
2. **Criteria API**:除了HQL,Hibernate还提供了Criteria API,这是一种更加面向对象的查询方式,允许动态构建查询条件,适用于那些查询条件在运行时不确定的情况。 3. **JPQL(Java Persistence Query Language...
Hibernate提供了两种主要的查询方式:离线查询(Criteria API)和HQL(Hibernate Query Language)语句查询。这两种查询方法各有特点,适用于不同的场景,使得开发者可以根据实际需求灵活选择。 1. Hibernate ...
总的来说,Hibernate的`Criteria`、`Query`以及QBC(Query By Criteria)提供了灵活且强大的查询手段,它们不仅支持基本的单表查询,还能处理复杂的联表、分页、参数绑定和统计需求,是Java开发者进行数据库操作的...
总之,Hibernate-HQL是Java开发中处理数据库查询的重要工具,通过理解和熟练运用HQL,可以显著提高开发效率,降低维护成本。通过深入学习和实践,开发者可以更好地驾驭这个强大的查询语言,提升项目开发的质量和效率...
除了HQL外,Hibernate还提供了Criteria API,这是一种更加面向对象的查询方式,可以动态构建查询条件。Criteria API提供了更丰富的查询选项,如 Restrictions、Projections 和 Order 等,使得代码更具灵活性。 总结...
Hibernate查询语言(HQL)是Java开发者用于操作Hibernate ORM框架中的对象关系映射数据的一种强大的查询工具。HQL是面向对象的,它允许开发者用类名和属性而不是表名和列名来编写查询,极大地提高了代码的可读性和可...
除了HQL,Hibernate还支持QBC查询方式,这是一种基于Criteria API的查询方式,提供了更加类型安全和灵活的查询构建器。通过Criteria API,开发者可以动态地构建复杂的查询条件,而不必关心具体的SQL语法。 ##### ...
本教程将深入探讨Hibernate中的两种查询方式:HQL(Hibernate Query Language)和QBC(Query By Example)。通过实例开发,我们将了解这两种查询方法的使用和它们之间的差异。 首先,HQL是Hibernate专门设计的一种...