HQL; List of Integer as input query - java

I have a List composed by Integer elements. I have to make a single query like this:
From Table as t where t.id <> element1 AND t.id <> element2 AND ......
Someone can give me a tip how to set the input list? I have to set the single element or the entire list?

Create Collection of Integers:
Collection<Integer> ints = new ArrayList<Integer>();
ints.add(1);
ints.add(2);
ints.add(3);
Set it as parameter:
Query q = entityManager.createQuery("FROM Table as t WHERE t.id NOT IN (:ints)");
q.setParameterList("ints", ints);
Relevant question: Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

Related

Hibernate Restriction to search with like clause in list

I need help with hibernate criteria restriction.
As we know that we use IN restriction with list , i.e
Criteria criteria = session.createCriteria(Student.class);
List<String> studentNames= new ArrayList();
criteria.add(Restrictions.in("name",studentNames);
List list = criteria.list();
and to use like
we use restriction like this i.e
String matchString = "Suraj Kumar";
criteria.add(Restrictions.like(
"studentName", matchString));
Problem: What i want is that every element in list should be searched with "LIKE" clause. If we use IN clause, then it compares the element with all the elements passed in the IN clause, and that comparison is based on equality.
But requirement is that , every element in the list should be checked with the LIKE clause , with the corresponding database column.
Please help me to create hibernate criteria restriction with the same.
Thanks
You can try to use Disjunction which can be used if you have multiple OR conditions.
Criteria criteria = session.createCriteria(Student.class);
List<String> studentNames= new ArrayList();
Disjunction disj = Restrictions.disjunction();
for (String value : studentNames) {
disj.add(Restrictions.like("name", value));
}
criteria.add(disj);
List list = criteria.list();

What is the return type for JPA query method when query result is not a class?

I have this query in JPA:
#Query("SELECT programId,COUNT(id) FROM Therapy GROUP BY programId ORDER BY COUNT(id) DESC")
List<Object> top10ProgramsOfTherapies();
It works great, but it returns a list of Objects, and I can not get the data out of it. What return type should I use to read the result data?
You can also create a DTO class, for example, TherapyDto which will have a constructor with 2 parameters and use it this way:
#Query("SELECT new com.my.TherapyDto(programId,COUNT(id)) FROM Therapy GROUP BY programId ORDER BY COUNT(id) DESC")
List<TherapyDto> top10ProgramsOfTherapies();
This query will return a list of objects array: Object[] so you need to change your code like this:
#Query("SELECT programId,COUNT(id) FROM Therapy GROUP BY programId ORDER BY COUNT(id) DESC")
List<Object[]> top10ProgramsOfTherapies();
And for each item in the list : item[0] will hold the programID value and item[1] will hold the COUNT(id) value, and you should cast them to their respective types as they will be just objects.

Return single column from multi-group Hibernate projection

What I want to achieve:
Select a susbset of entities that have a property value that exists in a List of values. This list is returned by another Query.
In plain SQL, this can be easily achieved with subqueries. This is the Criteria query that returns the relevant values:
DetachedCriteria subq = DetachedCriteria.forClass(MyClass.class)
.setProjection(
Projections.projectionList()
.add(Projections.alias(Projections.max("interestingVal"), "interestingVal"))
.add(Projections.groupProperty("someval1"))
.add(Projections.groupProperty("someval2"))
.add(Projections.groupProperty("someval3"))
);
I would then like to select the entities that have a value of interesting_val that was returned by the above query. Like so:
List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
.add(Subqueries.propertyIn("interestingVal", subq))
.list();
Unfortunately, the subq subquery returns a List of Object-arrays with length 4, since I have 4 Projection values. All projection values seem to be automatically added to the SELECT clause. This results in an
SQLSyntaxErrorException: ORA-00913: too many values.
How can I either tell my second Criteria query to only use the first element in the Object array or only retrieve the first column in my subquery?
Instead of propertyIn try propertiesIn.
String[] vals = new String[]{"interestingVal", "someval1", "someval2", "someval3"};
List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
.add(Subqueries.propertiesIn(vals, subq))
.list();

Hibernate: what type of object after query

How to get values from List element which contains UserTable and ResultTable data?
List<?> list = hSession.createQuery("FROM UserTable u, ResultTable r WHERE u.id = r.id AND r.ispassed = 1").list();
for(Object winner: winners) {
// ...
}
You will get a List of Object[], with each row having two entries - first one of type UserTable and second one of type ResultTable.
It would probably be faster for you to debug this on your own instead of posting a question.

Hibernal HQL is returning only one row

Below is the HQL query I'm using :
Session ssn=HibernateSessionFactory.openSession();
Query query = ssn.createQuery("from TUIDInfo e where e.id = :cred_id ");
query.setParameter("cred_id",cred_id);
List list = query.list();
My table contains around 20 entries matching the cred_id but this query only returns the last row in the table. Why is this so ? The query.list().size() attribute is always 1 even though there are lot more entries.

Categories