Take a look at this little tutorial on mastering Hibernate Query Language, HQL:
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries
From that example, you see how a hard coded HQL Query:
Code:
Session session = HibernateUtil.beginTransaction();
String hql="from User where loginName = 'mj' ";
Query query = session.createQuery(hql);
Object o = query.uniqueResult();
User u = (User)o;
System.out.println(u.getLoginName());
System.out.println("\n\n");
HibernateUtil.commitTransaction();
Can instead be parameterized and initialized, very similar to how a typical SQL PreparedStatement might do in in JDBC:
Code:
Session session = HibernateUtil.beginTransaction();
String loginName = "mj";
String hql="from User where loginName = :name";
Query query = session.createQuery(hql);
query.setString("name", loginName);
Object o = query.uniqueResult();
User user = (User)o;
System.out.println(user.getLoginName());
HibernateUtil.commitTransaction();
The tutorial even shows how you can do it as a Hibernate3 NamedQuery:
Code:
@NamedQuery(name="user.findByLoginName",
query="from User where loginName = :name")
Hope that helps you Hibernate!