0

I want to return two values from a function after executing query i.e. result & message

@Repository
@Transactional(value = "myTransactionManager")
public class UserFunctionsDAOImpl implements UserFunctionsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Object[]> registerUser(Users user) {
        // TODO Auto-generated method stub
        ReturnData returnData = new ReturnData(); 

        Query query = this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM Users WHERE emailID = :email_ID OR mobileNo = :mobile_No");
        query.setString("email_ID", user.getEmailID());
        query.setString("mobile_No", user.getMobileNo());

        if(((long)query.uniqueResult()) > 0)
        {
            returnData.setResult(false);
            returnData.setMessage("Email or Mobile number already exists");
            return (List<Object[]>);
        }
        else
        {
            Query query = this.sessionFactory.getCurrentSession().createSQLQuery("");
            query.executeUpdate();
            returnData.setResult(true);
            returnData.setMessage("User Registered Successfully");
            return (List<Object>[])returnData;
        }
    }

}

public class ReturnData
{
    Boolean result;
    String message;

    //getters and setters
}

Error

java.lang.ClassCastException: com.pir.model.ReturnData cannot be cast to java.util.List

How to solve this error?

1 Answer 1

2

You're returning the object that cannot be casted to the return type of the method.

Rewrite the method like this:

@Override
    public ReturnData registerUser(Users user) {
      // TODO Auto-generated method stub
    ReturnData returnData = new ReturnData(); 

    Query query = this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM Users WHERE emailID = :email_ID OR mobileNo = :mobile_No");
    query.setString("email_ID", user.getEmailID());
    query.setString("mobile_No", user.getMobileNo());

    if(((long)query.uniqueResult()) > 0)
    {
        returnData.setResult(false);
        returnData.setMessage("Email or Mobile number already exists");
        return returnData;
    }
    else
    {
        Query query =       this.sessionFactory.getCurrentSession().createSQLQuery("");
        query.executeUpdate();
        returnData.setResult(true);
        returnData.setMessage("User Registered Successfully");
        return returnData;
    }
}
Sign up to request clarification or add additional context in comments.

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.