Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate Criteria "fast" search

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way for me to do a following type search using hibernate Criteria/Restriction ---

Table "School" has a column "dept_id". I have a big array of "dept_id", is there a way for me to get all the rows whose "dept_id" are contained in my array of "dept_id". If we use SQL, we have to use syntax "WHERE dept_id IN (id1, id2,......).." which is tedious to create and is not elegant. Is there a faster and cleaner way for us to do it in Hibernate ?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can also use the IN clause in Hibernate. I recommend using HQL for it, and you use bind variables

so for example

Query query = session.createQuery("from School s Where s.deptId IN epts")

then use I think it is something like
query.setListParameter("depts",myListReference);

Mark
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark. Couple of more questions if you don't mind ---

1. do you think Criteria and Restriction can't do the same thing ? I guess they can't, but if you can verify this with me, will save me time researching on that.

2. HQL is cool. But so far I have only seen examples like "from MyTable where ..". It means select everything from the table. How can I do things like SQL "select T1.name, T2.address from T1, T2 where T1.id = T2.id and ..." ?? i.e. select a subset from tables

Can HQL do the same thing ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic