from:http://porsper.bokee.com/viewdiary.11756790.html
HQL: Hibernate查询语言(转自官方网站)
15.1. 大小写敏感性问题
除了Java类与属性的名称外,查询语句对大小写并不敏感。 所以 SeLeCT 与 sELEct 以及 SELECT 是相同的,但是 org.hibernate.eg.FOO 并不等价于 org.hibernate.eg.Foo 并且 foo.barSet 也不等价于 foo.BARSET。
本手册中的HQL关键字将使用小写字母. 很多用户发现使用完全大写的关键字会使查询语句 的可读性更强, 但我们发现,当把查询语句嵌入到Java语句中的时候使用大写关键字比较难看。
15.2. from子句
Hibernate中最简单的查询语句的形式如下:
代码内容 from eg.Cat |
代码内容 from Cat |
代码内容 from Cat as cat |
代码内容 from Cat cat |
15.3. 关联(Association)与连接(Join)
我们也可以为相关联的实体甚至是对一个集合中的全部元素指定一个别名, 这时要使用关键字join。
代码内容 from Cat as cat inner join cat.mate as mate left outer join cat.kittens as kitten from Cat as cat left join cat.mate.kittens as kittens from Formula form full join form.parameter param |
代码内容 from Cat as cat join cat.mate as mate left join cat.kittens as kitten |
代码内容 from Cat as cat inner join fetch cat.mate left join fetch cat.kittens |
代码内容 from Document fetch all properties order by name from Document doc fetch all properties where lower(doc.name) like ’%cats%’ |
代码内容 select distinct cat.name from Cat cat select count(distinct cat.name), count(cat) from Cat cat |
代码内容 from Cat as cat |
代码内容 from java.lang.Object o |
代码内容 from Named n, Named m where n.name = m.name |
代码内容 from Cat where name=’Fritz’ |
代码内容 from Cat as cat where cat.name=’Fritz’ |
代码内容 select foo from Foo foo, Bar bar where foo.startDate = bar.date |
代码内容 from Foo foo where foo.bar.baz.customer.address.city is not null |
代码内容 from Cat cat, Cat rival where cat.mate = rival.mate select cat, mate from Cat cat, Cat mate where cat.mate = mate |
代码内容 from Cat as cat where cat.id = 123 from Cat as cat where cat.mate.id = 69 |
代码内容 from bank.Person person where person.id.country = ’AU’ and person.id.medicareNumber = 123456 from bank.Account account where account.owner.id.country = ’AU’ and account.owner.id.medicareNumber = 123456 |
代码内容 from Cat cat where cat.class = DomesticCat |
代码内容 store.owner.address.city // 正确 store.owner.address // 错误! |
代码内容 from AuditLog log, Payment payment where log.item.class = ’Payment’ and log.item.id = payment.id |
代码内容 from DomesticCat cat where cat.name between ’A’ and ’B’ from DomesticCat cat where cat.name in ( ’Foo’, ’Bar’, ’Baz’ ) |
代码内容 from DomesticCat cat where cat.name not between ’A’ and ’B’ from DomesticCat cat where cat.name not in ( ’Foo’, ’Bar’, ’Baz’ ) |
代码内容 <property name="hibernate.query.substitutions">true 1, false 0</property> |
代码内容 from Cat cat where cat.alive = true |
代码内容 from Cat cat where cat.kittens.size > 0 from Cat cat where size(cat.kittens) > 0 |
代码内容 from Calendar cal where maxelement(cal.holidays) > current date from Order order where maxindex(order.items) > 100 from Order order where minelement(order.items) > 10000 |
代码内容 select mother from Cat as mother, Cat as kit where kit in elements(foo.kittens) select p from NameList list, Person p where p.name = some elements(list.names) from Cat cat where exists elements(cat.kittens) from Player p where 3 > all elements(p.scores) from Show show where ’fizard’ in indices(show.acts) |
代码内容 from Order order where order.items[0].id = 1234 select person from Person person, Calendar calendar where calendar.holidays[’national day’] = person.birthDay and person.nationality.calendar = calendar select item from Item item, Order order where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11 select item from Item item, Order order where order.items[ maxindex(order.items) ] = item and order.id = 11 在[]中的表达式甚至可以是一个算数表达式。 select item from Item item, Order order where order.items[ size(order.items) - 1 ] = item |
代码内容 select item, index(item) from Order order join order.items item where index(item) < 5 |
代码内容 select cust from Product prod, Store store inner join store.customers cust where prod.name = ’widget’ and store.location.name in ( ’Melbourne’, ’Sydney’ ) and prod = all elements(cust.currentOrder.lineItems) 提示: 会像如下的语句 SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order FROM customers cust, stores store, locations loc, store_customers sc, product prod WHERE prod.name = ’widget’ AND store.loc_id = loc.id AND loc.name IN ( ’Melbourne’, ’Sydney’ ) AND sc.store_id = store.id AND sc.cust_id = cust.id AND prod.id = ALL( SELECT item.prod_id FROM line_items item, orders o WHERE item.order_id = o.id AND cust.current_order = o.id ) |
代码内容 from DomesticCat cat order by cat.name asc, cat.weight desc, cat.birthdate |
代码内容 select cat.color, sum(cat.weight), count(cat) from Cat cat group by cat.color select foo.id, avg(name), max(name) from Foo foo join foo.names name group by foo.id having子句在这里也允许使用. select cat.color, sum(cat.weight), count(cat) from Cat cat group by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK) |
代码内容 select cat from Cat cat join cat.kittens kitten group by cat having avg(kitten.weight) > 100 order by count(kitten) asc, sum(kitten.weight) desc |
代码内容 from Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from DomesticCat cat ) from DomesticCat as cat where cat.name = some ( select name.nickName from Name as name ) from Cat as cat where not exists ( from Cat as mate where mate.mate = cat ) from DomesticCat as cat where cat.name not in ( select name.nickName from Name as name ) |
代码内容 from Cat as cat where not ( cat.name, cat.color ) in ( select cat.name, cat.color from DomesticCat cat ) |
代码内容 from Person where name = (’Gavin’, ’A’, ’King’) |
代码内容 from Person where name.first = ’Gavin’ and name.initial = ’A’ and name.last = ’King’) |
代码内容 select order.id, sum(price.amount), count(item) from Order as order join order.lineItems as item join item.product as product, Catalog as catalog join catalog.prices as price where order.paid = false and order.customer = :customer and price.product = product and catalog.effectiveDate < sysdate and catalog.effectiveDate >= all ( select cat.effectiveDate from Catalog as cat where cat.effectiveDate < sysdate ) group by order having sum(price.amount) > :minAmount order by sum(price.amount) desc |
代码内容 select order.id, sum(price.amount), count(item) from Order as order join order.lineItems as item join item.product as product, Catalog as catalog join catalog.prices as price where order.paid = false and order.customer = :customer and price.product = product and catalog = :currentCatalog group by order having sum(price.amount) > :minAmount order by sum(price.amount) desc |
代码内容 select count(payment), status.name from Payment as payment join payment.currentStatus as status join payment.statusChanges as statusChange where payment.status.name <> PaymentStatus.AWAITING_APPROVAL or ( statusChange.timeStamp = ( select max(change.timeStamp) from PaymentStatusChange change where change.payment = payment ) and statusChange.user <> :currentUser ) group by status.name, status.sortOrder order by status.sortOrder |
代码内容 select count(payment), status.name from Payment as payment join payment.currentStatus as status where payment.status.name <> PaymentStatus.AWAITING_APPROVAL or payment.statusChanges[ maxIndex(payment.statusChanges) ].user <> :currentUser group by status.name, status.sortOrder order by status.sortOrder |
代码内容 select account, payment from Account as account left outer join account.payments as payment where :currentUser in elements(account.holder.users) and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID) order by account.type.sortOrder, account.accountNumber, payment.dueDate |
代码内容 select account, payment from Account as account join account.holder.users as user left outer join account.payments as payment where :currentUser = user and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID) order by account.type.sortOrder, account.accountNumber, payment.dueDate |
代码内容 select usr.id, usr.name from User as usr left join usr.messages as msg group by usr.id, usr.name order by count(msg) |
代码内容 from User usr where size(usr.messages) >= 1 |
代码内容 select usr.id, usr.name from User usr.name join usr.messages msg group by usr.id, usr.name having count(msg) >= 1 |
代码内容 select usr.id, usr.name from User as usr left join usr.messages as msg group by usr.id, usr.name having count(msg) = 0 |
代码内容 Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size"); q.setProperties(fooBean); // fooBean包含方法getName()与getSize() List foos = q.list(); |
代码内容 Query q = s.createFilter( collection, "" ); // 一个简单的过滤器 q.setMaxResults(PAGE_SIZE); q.setFirstResult(PAGE_SIZE * pageNumber); List page = q.list(); |
代码内容 Collection orderedCollection = s.filter( collection, "order by this.amount" ); Collection counts = s.filter( collection, "select this.type, count(this) group by this.type" ); |
代码内容 ( (Integer) session.iterate("select count(*) from ....").next() ).intValue(); |
你可以通过这个链接引用该篇文章:http://porsper.bokee.com/viewdiary.11756790.html