Non-equal query in hql does not work

I am processing a non-equal request in hql.

@Override public Student findStudentsByYear(String year) { String queryString = "from Student where year<>:year "; Query query = sessionFactory.getCurrentSession().createQuery(queryString); query.setParameter("year", year); return (Student)query.uniqueResult(); } 

but it doesn’t work correctly. How to write this query correctly

Table of my students

 +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | studentId | bigint(20) | NO | PRI | NULL | auto_increment | | course | varchar(255) | YES | | NULL | | | dob | varchar(255) | YES | | NULL | | | email | varchar(255) | YES | | NULL | | | faculty | varchar(255) | YES | | NULL | | | firstName | varchar(255) | YES | | NULL | | | gender | int(11) | YES | | NULL | | | homeAddress | varchar(255) | YES | | NULL | | | lastName | varchar(255) | YES | | NULL | | | linkedIn | varchar(255) | YES | | NULL | | | university | varchar(255) | YES | | NULL | | | year | varchar(255) | YES | | NULL | | | user_userId | bigint(20) | YES | MUL | NULL | | +-------------+--------------+------+-----+---------+----------------+ 
+5
source share
1 answer

I know this late, but if anyone has a similar problem, you can use this:

 Criteria criteria = session.createCriteria(Student.class); criteria.add(Restrictions.ne("year", year)); List<Student> result = criteria.list(); 

Or that:

 List<Student> result = session.createQuery ("from Student where year!=:year").setParameter("year", year).list(); 

I'm not sure the problem is in the above example, since Samantha did not provide any information that it was never like this, but I assume that uniqueResult() is causing problems because this query returns a list, not a single result.

+12
source

Source: https://habr.com/ru/post/1214692/


All Articles