Hibernate HQL Query : How to set a Collection as a named parameter of a Query?
Use Query.setParameterList(), Javadoc here. There are four variants to pick from.
Use Query.setParameterList(), Javadoc here. There are four variants to pick from.
The “fetch” tells hibernate to load it now instead of letting it be loaded lazily. The reference guide has a whole chapter dealing with such things that it’s good to be acquainted with.
It seems like your query returns more than one result check the database. In documentation of query.uniqueResult() you can read: Throws: org.hibernate.NonUniqueResultException – if there is more than one matching result If you want to avoid this error and still use unique result request, you can use this kind of workaround query.setMaxResults(1).uniqueResult();
I’m the guy who wrote the Hibernate 3 query translator back in 2004, so I know something about how it works. Criteria, in theory should have less overhead than an HQL query (except for named queries, which I’ll get to). This is because Criteria doesn’t need to parse anything. HQL queries are parsed with an … Read more
You need to create an alias for the mother.kind. You do this like so. Criteria c = session.createCriteria(Cat.class); c.createAlias(“mother.kind”, “motherKind”); c.addOrder(Order.asc(“motherKind.value”)); return c.list();
This is indeed possible with criteria: DetachedCriteria ownerCriteria = DetachedCriteria.forClass(Owner.class); ownerCriteria.setProjection(Property.forName(“id”)); ownerCriteria.add(Restrictions.eq(“ownername”, “bob”)); Criteria criteria = getSession().createCriteria(Pet.class); criteria.add(Property.forName(“ownerId”).in(ownerCriteria)); Update: This actually performs a sub-query instead of a join but it allows you to use Criteria on two entities that do not have a hibernate relationship defined.
billProductSet is a Collection. As such, it does not have an attribute named product. Product is an attribute of the elements of this Collection. You can fix the issue by joining the collection instead of dereferencing it: SELECT count(*) FROM BillDetails bd JOIN bd.billProductSet bps WHERE bd.client.id = 1 AND bps.product.id = 1002
I am unsure how to do this with positional parameter, but if you can use named parameters instead of positional, then named parameter can be placed inside brackets and setParameterList method from Query interface can be used to bind the list of values to this parameter. … Query query = session.createQuery(“FROM Cat c WHERE c.id … Read more
Are you using Hibernate’s Query object, or JPA? For JPA, it should work fine: String jpql = “from A where name in (:names)”; Query q = em.createQuery(jpql); q.setParameter(“names”, l); For Hibernate’s, you’ll need to use the setParameterList: String hql = “from A where name in (:names)”; Query q = s.createQuery(hql); q.setParameterList(“names”, l);
Here’s a snippet of hql that we use. (Names have been changed to protect identities) String queryString = “select distinct f from Foo f inner join foo.bars as b” + ” where f.creationDate >= ? and f.creationDate < ? and b.bar = ?”; return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});