Tutorial :Hibernate for getting a list of primitive integers for subselect



Question:

Isn't there a way with Hibernate to return a list of (primitive) values from one column in a table? I need this for a subselect where I only want rows where a particular field is not in a list of ids from another table.


Solution:1

Can you use a Hibernate raw SQLQuery?

SQLQuery q = getSession().createSQLQuery("select int_column from table");  List<Integer> list = (List<Integer>) q.list();  


Solution:2

well it turned to be as simple as something such as the following, from the URL https://www.hibernate.org/hib_docs/nhibernate/html/queryhql.html which was actually one of the first results I found when googling but I was concerned it might be NHibernate specific

from Eg.DomesticCat as cat where cat.Name not in (   select name.NickName from Eg.Name as name )  


Solution:3

Dont know about using the hibernate engine itself, I think this will depend on how youve mapped your model objects, short of seeing the mappings, you can go via the hibernate session object and use standard SQL;

session.createSQLQuery("select idCol from someTable where someId not in (  select someId from anotherTable)");  

Then use the list() method on the query, and use autoboxing for the primtive int array.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »