I'm wondering if is possible to make this kind of evaluation with collections like the query isn't working in my project at a class named query as follow:
#NamedQueries(value = {
#NamedQuery(
name = "Foo.getBarList",
query = "select f from Foo f where (:barlist is null or f.barlist.id in (:barslist)) " )})
from this both post:
FROM Foo WHERE Id = :id AND (:barlist is null or Bar in (:barlist))
JPA where clause any
Hibernate HQL Query : How to set a Collection as a named parameter of a Query?
I guess the problem was the id in query so I remove and the error stop, just change this:
"select f from Foo f where (:barlist is null or f.barlist in (:barslist)) "
instead of in HQL you can check
if(barlist==null){
//query=FROM Foo WHERE Id = :id
}else{
//query=FROM Foo WHERE Id = :id AND Bar in :barlist
// query.setParameterList(:barlist",<list object>)
}
Related
I have query which filters items by certain conditions:
#NamedQueries({
#NamedQuery(
name = ITEM.FIND_ALL_PARAMS_BY_COMPANY_COUNTRY_GROUP,
query = "SELECT i FROM Item i where "
+ "((i.idCompany=:companyId AND i.idEMGroup=:groupId) "
+ "OR (i.idCompany=:companyId AND i.idEMCountry =:countryId AND i.idEMGroup is null) "
+ "OR (i.idCompany is null AND i.idEMCountry = :countryId AND i.idEMGroup is null)) "
+ "order by i.idEMCountry desc, i.idCompany desc, i.idEMGroup desc")
})
In some cases parameters idEMGroup o companyId can be null which generates sql looking like this IdEmCompany = 200630758) AND (IdEMGroup = NULL) and it is incorrect sql syntax is it possible to dynamically if value is null for it as 'Column IS NULL' instead of 'Column = NULL' without adding a lot of if's, or it's just better to rewrite this query using Criteria API and just check if value is present and add predicates on certain conditions ?
Correct answer would be to use CriteriaQuery.
Though it is also possible to construct the query dynamically but manipulating #NamedQuery is not possible or might require stuff that makes it not worth to do.
Instead you could construct the query first as a String and create TypedQuery by manipulating the query string
String strQuery = "SELECT i FROM Item i"; // .. + the rest of stuff
if(null==companyId) {
// add something like "companyId IS :companyId"
// ":companyId" coulöd also be NULL"
// but to enable using tq.setParameter("companyId", companyId)
// without checking if there is param "companyId" so there always will
} else {
// add something like "companyId=:companyId"
}
TypedQuery<Item> tq = entityManager.createQuery(strQuery, Item.class);
tq.setParameter("companyId", companyId);
There will be some IFs but so will be in CriteriaQuery construction also.
I have an Entity class called Pilot with the following defined named query:
#NamedQuery(name="Pilot.findById", query="SELECT p FROM Pilot p where p.id = :id")
And another one called Flight with this named query:
#NamedQuery(name="Flight.findById", query="SELECT f FROM Flight f where f.id = :id")
Then i have a method that does the following:
TypedQuery<Flight> fQuery = em.createNamedQuery("Flight.findById", Flight.class);
fQuery.setParameter("id", Integer.parseInt(flightId));
Flight f = fQuery.getSingleResult();
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
fQuery.setParameter("id", Integer.parseInt(pilotId));
Pilot p = pQuery.getSingleResult();
But for some reason when i call it i get the error:
...
Caused by: java.lang.IllegalStateException: Query argument id not found in the list of parameters provided during query execution.
...
What could be the problem?
Looks like a cut and paste error...
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
fQuery.setParameter("id", Integer.parseInt(pilotId));
^^^^^^
You are setting the id on fQuery twice. Try changing the one for pilots to pQuery. Like this:
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
pQuery.setParameter("id", Integer.parseInt(pilotId));
^^^^^^
Here municipalities parameter can be null. but IN clause doesnt work for null. If there was only one paramater i.e municipalities, I could have handle this in java level. but there is another condition in OR, which has to be executed.
List<Municipality> municipalities = myDao.findAll(); // returns empty list
em.createQuery("SELECT p FROM Profile p JOIN p.municipality m WHERE m IN (:municipalities) OR m.city = :city")
.setParameter("municipalities", municipalities)
.setParameter("city", city)
.getResultList();
So how to do this? Can we handle null in JPA IN clause. like if it is null then dont execute that condition
you can modify the query
String sql="";
if(municipalities!=null){
sql="SELECT p FROM Profile p JOIN p.municipality m WHERE m IN (:municipalities) OR m.city = :city"
}
else
{
sql="SELECT p FROM Profile p JOIN p.municipality m WHERE m.city = :city"
}
List<Municipality> municipalities = myDao.findAll(); // returns empty list
em.createQuery(sql)
.setParameter("municipalities", municipalities)
.setParameter("city", city)
.getResultList();
My Query is this:
query1 = select a.id from entity1 a where a.id in (:List1)
and not exists (select ex2 from entity2 ex2 where ex2.assignedId = a.id)
union
select ex.assignedId from entity2 ex ,entity3 pi
where ex.entity3Id = pi.id and ex.assignedId in (:List1)
and ex.assignedTypeId = :assignedTypeId and pi.processStatus = :status
and not exists
(select ex1.assignedId from entity2 ex1 , entity3 pi1
where ex1.entity3Id = pi1.id and ex1.assignedId = ex.assignedId
and ex1.assignedTypeId = :assignedTypeId
and pi1.processStatus <> :status);
and while trying to execute query,
Query existingIds=em.createQuery(query1); //With all parameters set
throws NullPointerException in line 87 of org.hibernate.hql.ast.ParameterTranslationsImpl
completely checked all the braces and parameters. The equivalent conversion works in mysql.
Can someone assist me in converting the query with CriteriaBuilder, finding it difficult to make the conversion.
Not sure if JPQL supports union operation at all. Are you putting this as NamedQuery or you are creating on the fly (entityManager.createQuery()) ?
I want to construt a hql query like
select PLAN_ID from "GPIL_DB"."ROUTE_PLAN" where ASSIGNED_TO
in ('prav','sheet') and END_DATE > todays date
I am doing in this way but getting an error in setting parameters
s=('a','b');
Query q = getSession().createQuery("select planId from RoutePlan where assignedTo in REG ");
if(selUsers != null) {
q.setParameter("REG", s);
}
where i am doing wrong? Please help in executing this hwl based query having in clause
You need to assign the parameter list in the query. Also note the brackets around the parameter because it is an 'in' query.
Query q = getSession()
.createQuery("select planId from RoutePlan where assignedTo in (:REG) ");
if(selUsers != null) {
q.setParameterList("REG", s);
}
You can read more about how to use parameters in HQL in the hibernate reference, but this is the relevant example pasted from there:
//named parameter list
List names = new ArrayList();
names.add("Izi");
names.add("Fritz");
Query q = sess.createQuery("from DomesticCat cat where cat.name in (:namesList)");
q.setParameterList("namesList", names);
List cats = q.list();