Barrier Barrier - 2 months ago 7
SQL Question

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 ?

Answer

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