1

I have the code:

public Student LoginStudent(Student student) {              
    List<Student> students = new ArrayList<Student>();
    sessionFactory.getCurrentSession().getTransaction().begin();
    String hql = "select stu_id,name from  student  where username = \"[email protected]\"";
    students = (List<Student>) sessionFactory.getCurrentSession().createSQLQuery(hql).uniqueResult();               
    if (students.size() > 0) {
        return students.get(0);
    } else {
        return null;
    }
}

I am getting error:

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.util.List

I searched Google, someone suggests:

return ((BigInteger)LoginStudent.get(0)).longValue();

But how can I use this?

1
  • When you run the sql "select stu_id,name from student where username = "[email protected]" what is the result you are getting? Commented Aug 9, 2015 at 10:03

4 Answers 4

2

Change hql to

String hql = "from Student where username = '[email protected]'";
students = (List<Student>) sessionFactory.getCurrentSession().createQuery(hql).list();

the javadoc for uniqueResult says

Convenience method to return a single instance that matches the query, or null if the query returns no results.

But you need a List<Student> to retrieve, so use list().

Sign up to request clarification or add additional context in comments.

4 Comments

I have used list().....I am getting error:---java.math.BigInteger cannot be cast to com.student.pack.Student
return students.get(0);-----this line
students is a List<Student>, and you return the first element of it which is a Student. There's no BigInteger here, unless you manually cast it. Don't cast an object which is not instance of a BigInteger to this type. And use proper naming in code, if you write hql then use HQL, if you want to use SQL, then write sql. I will add the line to execute HQL instead of SQL, and read this if you still want to use SQL.
@Salini: sounds like you didn't use Roman's change to the hql, try that first.
0

Please change the line from

students = (List<Student>) sessionFactory.getCurrentSession().createSQLQuery(hql).uniqueResult();

to

students = (List<Student>) (sessionFactory.getCurrentSession().createSQLQuery(hql).uniqueResult());

Give a try, it should work if uniqueResult() returns a list of student objects

Comments

0

To get your hql to work you need to do something like this:

Student student = null;

sessionFactory.getCurrentSession().getTransaction().begin();
String hql = "select stu_id,name from  student  where username = :username";

Object[] result = sessionFactory.getCurrentSession()
    .createSQLQuery(hql)
    .setParameter("username", "[email protected]")
    .uniqueResult();

if(result != null) {
   // manually convert your selection into a Student
   student = new Student();
   s.setId((Long) result[0]);
   s.setName((String) result[1]);
}

return student;

The reason you have to do this is because you are selecting certain field from Student in your hql query. See https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

Comments

0

Or declare a constructor so your query works like this:

select new Student(s.stu_id,s.name) from  Student s where...

That should do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.