I have a small query something like this:
String sqlStr = "select count(*) from MyTable n where primaryKey.userId = :userId and primaryKey.userType = :userType"
String[] paramNames = {"userId", "userType" };
Object[] paramValues = {userId, userType};
List myObjects = (List) hibernateTemplate.findByNamedQueryAndNamedParam("objectsToDeleteForUser", paramNames, paramValues);
and this works fine. I can make a call with a userid and usertype and get one or more matching records.
What I'd like to do now that is have a list of userid's and usertype's and perform the call but not have to iterate over the list but make one call. So basically when userid(0) and usertype(0), userid(1) and usertype(1) ...................
Is it possible to do this with one call in HQL. Something like the IN clause (X).
Thanks
somwthing like this is possible :
hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
hibernateSession.beginTransaction();
List<Object> userId = new ArrayList<Object>();
List<Object> userType= new ArrayList<Object>();
Query q1 = hibernateSession.createQuery("select count(*) from MyTable n where primaryKey.userId IN (:userId) and primaryKey.userType IN (:userType)");
q1.setParameterList("userId", userId);
q1.setparameterList("userType" , userType);
List myObjects = q1.list();
hibernateSession.close();
this should probably work for you
Related
I am not getting output when I am directly passing a list through IN clause, but getting output when passing the values separately.
Working Code below:
List<String> resultList = null;
List<String> list = new ArrayList();
list.add("123");
list.add("456"):
String query = "select * from company where id in('123','456')");
resultList=getJdbcTemplate().queryForList(query, String.class);
Not working code:
List<String> resultList = null;
List<String> list = new ArrayList();
list.add("123");
list.add("456"):
String query = "select * from company where id in(:list)");
resultList=getJdbcTemplate().queryForList(query, String.class);
I want to fetch the query based on the list passed in the query. Could someone help me here
I have created a pojo class Company.java where I have mentioned db column fields.
After the query part,
List<Company> comp = getJdbcTemplate().queryForList(query, Company.class);
Error I am getting :
Incorrect Column count: expected 1, actual 23
What changes I need to do in above line.
I need to select distinct id and also need another column
MyClass class1 = new MyClass();
Criteria criteria = new Criteria(MyClass.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("col1"));
projList.add(Projections.property("col2"));
criteria.setProjection(Projections.distinct(projList));
class1 = criteria.list();
What will be the return type of criteria.list()?
If i try to assign it to MyClass.class I get ClassCast Exception.
Please assist. How will I get both my columns?
The result of criteria.list() must be a List<Object[]>, so:
List<Object[]> result = criteria.list();
for (Object[] row : result) {
Object col1 = row[0];
Object col2 = row[1];
}
I want to get distinct values from my db.
I have 10 fields in this db, and when i try to use such query:
SELECT DISTINCT (IMIE)FROM `przychodzace`
I get 26 results, but hibernate returns me just 17...
Here is list function:
List<String> list = new ArrayList<String>();
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Criteria criteria = session.createCriteria(PrzychodzaceModel.class);
if (i == 0) {
criteria.setProjection(Projections.distinct(Projections.property("imie")));
criteria.addOrder(Order.asc("imie"));
}
list = criteria.list();
System.out.println(list.size() + "size");
return list;
Have anyone idea how to do it properly, i am trying to correct it for long time.
Thanks in advance.
I think an alternative to your problem would be to use a DAO class with a List method, eg:
public List listMenu() {
String hql = "FROM Menu";
org.hibernate.Query query = session.getCurrentSession().createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(5);
List results = query.list();
return results;
}
i need the hql query that should return the Map as result, I tried hql new map query but it returns the list of map like follows
Session session = sessionFactory.getCurrentSession();
String HQL_QUERY = "select new map(user.id as id, user.fullName as fullName)
from User user";
List<Map<String,String>> usersList = session.createQuery(HQL_QUERY).list();
if this is the only solution then how do i convert a list of map into a single map without looping, because if the query returns more rows then the looping take more time for convertion. Help me.
I would suggest using Criteria and then a result transformer to create a map. Have a look at this for official documentation. This gives you a clue and you can find more samples on net.
Creating a map is not the job of HQL. It's your job. Simply loop over the rows you get from the query:
String hql = "select user.id, user.fullName from User user";
List<Object[]> rows = session.createQuery(hql).list();
Map<String, String> result = new HashMap<>();
for (Object[] row : rows) {
result.put((String) row[0], (String) row[1]);
}
you can use map like below
Query query = session.createQuery("select new map(id,username) from UserDetails");
List<?> idUsernameList=query.list();
Iterator<?> iterator = idUsernameList.iterator();
Map row=null;
while(iterator.hasNext()){
row=(Map)iterator.next();
System.out.println(row);
}
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();