0

This is my user.java domain class is

public class User {

private Integer iduser;
private String user_name;
private String user_activation_key;
private String user_password;
private String user_email;
private Character registered;

public Integer getIduser() {
    return iduser;
}

public void setIduser(Integer iduser) {
    this.iduser = iduser;
}

public String getUser_name() {
    return user_name;
}.....more code

And in a another Test class I have Coded as follow.

SQLQuery query = session.createSQLQuery("select * from user");
List<User> availableUsers = new ArrayList<User>();
availableUsers=(List<User>)query.list();

query is a SQLQuery (Hibernate implementation) to get user list from DB.
But I can't cast the (query.list) to List<User>.
I'm getting class cast exception. availableUser is a object list.
What steps should I follow.

2 Answers 2

2

Use HQL

SQLQuery query = session.createQuery("from User"); //note here User is POJO class name
List<User> availableUsers = new ArrayList<User>();
availableUsers=(List<User>)query.list();  

18.1.1. Scalar queries

These will return a List of Object arrays (Object[]) with scalar values for each column in the user table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.

Use addScalar() method when using createSQLQuery see more

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

6 Comments

I used HQL --> Query query = session.createQuery("from user"); and now Im getting a SQLGrammarException
Exception Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from' at line 1. mysql version is 5.5 and hbernate version is 4.3.0
@amilaShashin : do not use session.createSQLQuery(). See my answer it is session.createQuery().
I have used session.createQuery(); :)
@amilaShashin : Is query like from User? Note that U is in upper case. If this doesn't solve the problem please edit the question and paste the error.
|
1

query.list() returns List<Object>. Each object is actually an Object array (Object[]) which has the whole row.

For example, if you are getting all the Employee results, then the list is as follows:-

{ {emp_id,emp_name,other_emp_col} , {emp_id,emp_name,other_emp_col}, {emp_id,emp_name,other_emp_col} ....}

So for retrieving them you will have to iterate the list of Object and cast iterator.next() into an Object array and then construct your own User object.

For example:-

List<Object> userList = query.list();

Iterator<Object> itr = userList.iterator();
while(itr.hasNext()){
    Object[] userRow = (Object[]) itr.next();
    User userObj = new User();
    userObj.setName(userRow[0]);

}

To save all these efforts as Aniket said, use HQL

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.