gpt4 book ai didi

java - hibernate : How to use sql to retrieve a predefined object ?

转载 作者:行者123 更新时间:2023-12-01 16:51:19 25 4
gpt4 key购买 nike

我使用以下 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 对象 - 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

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

最佳答案

如果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

关于java - hibernate : How to use sql to retrieve a predefined object ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com