Hibernate:如何使用sql来检索预定义的对象?

时间:2016-09-19 04:37:59

标签: java sql hibernate

我使用以下sql来检索预定义的对象。

select idpatient, password from Patient where user_name= :username

这是我用来获取Patient对象的方法。

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;
}

这是我的患者对象 - 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;
    }

}

当我在 getUserNameAndPassword 方法上方运行时,会生成以下异常。

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

我怎么能正确地做到这一点?有什么想法吗?

4 个答案:

答案 0 :(得分:2)

如果Patient类是非托管实体,您可以使用

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

详细资料文件: 13.1.5. Returning non-managed entities

对于托管实体,您可以在选择查询中使用NEW关键字。

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

请注意,您必须先定义构造函数。

详细信息文档:15.6. The select clause

答案 1 :(得分:1)

试试这个,

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();

这看起来像是一个hibernate实现的构造函数表达式。

答案 2 :(得分:1)

您正在获取java.lang.ClassCastException,因为您正在重新检索仅具有两个属性的患者对象,即 idpatient,密码。但是,在Patient类中缺少用于创建此对象的构造函数。

以下查询检索具有Patient类的两个属性( idpatient,密码)的Patient对象。

ng g c recipe/recipe-item

您需要使用 idpatient,密码字段在Patient Class中创建参数化构造函数。

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

现在,您可以使用两个字段检索患者对象。

注意:如果您使用默认构造函数创建对象,则还需要创建一个默认的Patient Class构造函数以及参数化的构造函数。

答案 3 :(得分:1)

您也可以使用Hibernate criteria作为解决方案

   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;
    }
}