发布网友 发布时间:2022-04-20 22:27
共3个回答
热心网友 时间:2022-05-02 01:54
HQL:Hibernate Query Language
HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征。
HQL查询依赖于Query类,每个Query实例对应一个查询对象,使用HQL查询按
如下步骤进行:
(1)获取Hibernate Session对象;
(2)编写HQL语句;
(3)以HQL语句作为参数,调用Session的createQuery方法创建查询对象;
(4)如果HQL语句包含参数,调用Query的setXxx方法为参数赋值;
(5)调用Query对象的list等方法遍历查询结果。
查询示例:
public class HqlQuery
{
public static void main(String[] args) throws Exception {
HqlQuery mgr = new HqlQuery();
//调用查询方法
mgr.findPersons();
//调用第二个查询方法
mgr.findPersonByHappenDate();
HibernateUtil.sessionFactory.close();
}
//第一个查询方法
private void findPersons() {
//获得Hibernate Session
Session sess = HibernateUtil.currentSession();
//开始事务
Transaction tx = sess.beginTransaction();
//以HQL语句创建Query对象
//执行setString方法为HQL语句的参数赋值
//Query调用list方法访问查询的全部实例
List p1 = sess.createQuery("from Person p where o.myEvents.title = :
eventTitle").setString("eventTitle", "很普通事情").list();
//遍历查询的全部结果
for (Iterator pit = p1.iterator(); pit.haxNext(); )
{
Person p = (Person)pit.next();
System.out.println(p.getName());
}
//提交事务
tx.commit();
HibernateUtil.closeSession();
}
热心网友 时间:2022-05-02 03:12
回答人的补充 2009-07-07 15:21
4.多态查询 from Person as p from java.lang.Object o from Named as n 5.where子句 from Person where name like "tom%" from Person as p where p.name like "tom%" from Cat cat where cat.mate.name like "kit%" select * from cat_table as table1 cat_table as table2 where table1.mate = table2.id and table1.name like "kit%" from Foo foo where foo.bar.baz.customer.address.city like "fuzhou%" 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 Person as person where person.id.country = 'AU' and person.id.medicareNumber = 123456 from Account as account where account.owner.id.country = 'AU' and account.owner.id.medicareNumber = 123456 from Cat cat where cat.class = DomesticCat from Account as a where a.person.name.firstName like "dd%" // 正确 from Account as a where a.person.name like "dd%" // 错误
热心网友 时间:2022-05-02 04:46
from a left join c on a.id=c.a_id left join b on c.b_id=b.id