Hibernate Search method:
一。 HQL
from Cat --- Cat是类名
from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight
> 10.0
1.Join
2. select 子句
select mate
from Cat as cat
inner join cat.mate as mate
2.1查询语句可以返回多个对象和(或)属性,存放在 Object[] 队列中;
2.2存放在一个 List 对象中;
2.3类中;
Family 有一个合适的构造函数 - 作为实际的类型安全的 Java 对象:
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
2.4 Map
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Cat cat
2.5. order by; group by
3. 批量的 UPDATE 和 DELETE
HQL UPDATE 语句,默认不会影响更新实体的 version 或 the timestamp 属性值。这和 EJB3 规
范是一致的。但是,通过使用 versioned update,你可以强制 Hibernate 正确的重置version 或者
timestamp 属性值。这通过在 UPDATE 关键字后面增加 VERSIONED 关键字来实现的。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlVersionedUpdate = "update versioned Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
二。 条件查询(Criteria Queries)
2.1 限制结果集内容
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.add( Restrictions.between("weight", minWeight, maxWeight) )
.list();
Example example = Example.create(cat)
.excludeZeroes() //exclude zero valued properties
.excludeProperty("color") //exclude the property named "color"
.ignoreCase() //perform case insensitive string comparisons
.enableLike(); //use like for string comparisons
List results = session.createCriteria(Cat.class)
.add(example)
.list();