Terance Wijesuriya
Terance Wijesuriya

Reputation: 1986

Hibernate : How to use sql to retrieve a predefined object ?

I used following sql to retrieve a predefined object .

select idpatient, password from Patient where user_name= :username

And this is my method which I used to get a Patient object.

public Patient getUserNameAndPassword(String username, Session session) {
        Query query=session.createQuery("select idpatient, password from Patient where user_name= :username");
        query.setParameter("username", username);
        List list = query.list();
        Patient patient=(Patient) list.get(0);
        return patient;
}

This is my Patient object - Patient.java

public class Patient implements java.io.Serializable {

    private Integer idpatient;
    private String firstName;
    private String lastName;
    private String email;
    private Date dob;
    private String parentEmail;
    private String gender;
    private String userName;
    private String password;

    /**
     * @return the idpatient
     */
    public Integer getIdpatient() {
        return idpatient;
    }

    /**
     * @param idpatient the idpatient to set
     */
    public void setIdpatient(Integer idpatient) {
        this.idpatient = idpatient;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the dob
     */
    public Date getDob() {
        return dob;
    }

    /**
     * @param dob the dob to set
     */
    public void setDob(Date dob) {
        this.dob = dob;
    }

    /**
     * @return the parentEmail
     */
    public String getParentEmail() {
        return parentEmail;
    }

    /**
     * @param parentEmail the parentEmail to set
     */
    public void setParentEmail(String parentEmail) {
        this.parentEmail = parentEmail;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

}

When I run above getUserNameAndPassword method , following exception is generated .

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to beans.Patient

How could I do this correctly ? Have any ideas ?

Upvotes: 0

Views: 64

Answers (4)

Afsun Khammadli
Afsun Khammadli

Reputation: 2068

You can also use Hibernate criteria for solution

   public Patient getUserNameAndPassword(String username, Session session) {
    Criteria criteria = session.createCriteria(Patient.class);
    criteria.add(Restrictions.eq("username", username));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("idpatient"));
    projectionList.add(Projections.property("password"));
    criteria.setProjection(projectionList);
    criteria.setResultTransformer(new AliasToBeanResultTransformer(Patient.class));
    if (!criteria.list().isEmpty()) {
        Patient patient = (Patient) list.get(0);
        return patient;
    } else {
        return null;
    }
}

Upvotes: 1

Kushan
Kushan

Reputation: 10695

Try this,

Query query= session.createQuery("SELECT NEW beans.Patient( idpatient, password) FROM Patient WHERE user_name= :username");
query.setParameter("username", username);
List<Patient> patients = query.list();

This is looks like a hibernate implemented constructor expression.

Upvotes: 1

Micheal Vu
Micheal Vu

Reputation: 1478

if Patient class is non-managed entity, you can use

session.createSQLQuery("SELECT idpatient, password FROM Patient WHERE username=:user_name")
    .setParameter("user_name",username)
    .setResultTransformer(Transformers.aliasToBean(Patient.class))

Detail document here: 13.1.5. Returning non-managed entities

For managed entities, you can use NEW keyword in Select query.

List<Patient> patients = session.createQuery("SELECT NEW beans.Patient( idpatient, password) FROM Patient WHERE username='" + username + "'").list();

Take note that you have to define the constructor first.

Detail document here: 15.6. The select clause

Upvotes: 2

Rohit Gaikwad
Rohit Gaikwad

Reputation: 3904

You are getting the java.lang.ClassCastException because, you are retriving the patient object having only two properties i.e idpatient, password. But, a constructor to create this object is missing in Patient class.

The below query retrieves an Patient object having two properties (idpatient, password) of Patient class.

Query query=session.createQuery("select idpatient, password from Patient where user_name= :username");

You need to create a parameterized constructor in Patient Class using idpatient, password fields.

public Patient(Integer idpatient,String password ){
 this.idpatient=idpatient;
 this.password =password ;
}

Now, you can retrieve the Patient Object with two fields.

Note: If you are creating objects using default constructor, you also need to create a default Patient Class constructor along with the parameterized one.

Upvotes: 1

Related Questions