我正在通过Hibernate运行查询,并且正在返回列表。但是列表中包含对象而不是我的实体...我不是Hibernate的专业人士,但是我的其他查询运行良好。我不知道我现在的错误是什么。
List getAllUsersBasedOnMandant(Long id) {
List users = null;
try {
startOperation(false);
Query query = getSession().createQuery(" from UserEntity where mandant = '" + id + "'");
users = query.list();
} catch (HibernateException e) {
handleException(e);
} finally {
getSession().close();
}
return users;
}
我的输出如下:
[int_plan.entity.UserEntity @ 4b82b237,int_plan.entity.UserEntity @ 7141a0bb,int_plan.entity.UserEntity @ 65b0a12c]
我的实体看起来像这样:
@Entity
@Table(name = "user", schema = "entw_pares")
public class UserEntity {
@Expose() private Long userId;
@Expose() private String gender;
@Expose() private String firstname;
@Expose() private String lastname;
@Expose() private String username;
@Expose() private String email;
private String password;
private String secQuestion;
private String secAnswer;
private String saltAnswer;
private String salt;
private String emailValidationCode;
private Long expireTime;
private Boolean emailEnable = false;
@Expose() private Timestamp dateCreated;
@Expose() private Timestamp dateUpdated;
private Boolean admin = false;
private MandantEntity mandantEntity;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Basic
@Column(name = "gender", nullable = false)
public String getGender() { return gender; }
public void setGender(String gender){
this.gender = gender;
}
@Basic
@Column(name = "firstname", nullable = false)
public String getFirstname() { return firstname; }
public void setFirstname(String firstnme) { this.firstname = firstnme; }
@Basic
@Column(name = "lastname", nullable = false)
public String getLastname() { return lastname; }
public void setLastname(String lastname) { this.lastname = lastname; }
@Basic
@Column(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "email", nullable = false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Basic
@Column(name = "password", nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Basic
@Column(name = "sec_question")
public String getSecQuestion() { return secQuestion; }
public void setSecQuestion(String sec_question) { this.secQuestion =
sec_question; }
@Basic
@Column(name = "sec_answer")
public String getSecAnswer() { return secAnswer; }
public void setSecAnswer(String secAnswer) { this.secAnswer = secAnswer; }
@Basic
@Column(name = "salt_answer")
public String getSaltAnswer() { return saltAnswer; }
public void setSaltAnswer(String saltAnswer) { this.saltAnswer = saltAnswer;
}
@Basic
@Column(name = "salt")
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
@Basic
@Column(name = "email_validation_code")
public String getEmailValidationCode() {
return emailValidationCode;
}
public void setEmailValidationCode(String emailValidationCode) {
this.emailValidationCode = emailValidationCode;
}
@Basic
@Column(name = "expire_time", nullable = false)
public Long getExpireTime() {
return expireTime;
}
public void setExpireTime(Long expireTime) {
this.expireTime = expireTime;
}
@Basic
@Column(name = "email_enable")
public Boolean getEmailEnable() {
return emailEnable;
}
public void setEmailEnable(Boolean emailEnable) {
this.emailEnable = emailEnable;
}
@Basic
@CreationTimestamp
@Column(name = "date_created")
public Timestamp getDateCreated() {
return dateCreated;
}
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}
@Basic
@UpdateTimestamp
@Column(name = "date_updated")
public Timestamp getDateUpdated() {
return dateUpdated;
}
public void setDateUpdated(Timestamp dateUpdated) {
this.dateUpdated = dateUpdated;
}
@Basic
@Column(name = "admin")
public Boolean getAdmin() {
return admin;
}
public void setAdmin(Boolean admin) {
this.admin = admin;
}
public void applyValue(Field field, Object value) throws
IllegalAccessException {
field.set(this, value);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
return userId == that.userId &&
Objects.equals(gender, that.gender)&&
Objects.equals(firstname,that.firstname)&&
Objects.equals(lastname,that.lastname)&&
Objects.equals(username, that.username) &&
Objects.equals(email, that.email) &&
Objects.equals(password, that.password) &&
Objects.equals(secQuestion,that.secQuestion) &&
Objects.equals(secAnswer, that.secAnswer) &&
Objects.equals(salt, that.salt) &&
Objects.equals(emailValidationCode, that.emailValidationCode) &&
Objects.equals(emailEnable, that.emailEnable) &&
Objects.equals(dateCreated, that.dateCreated) &&
Objects.equals(dateUpdated, that.dateUpdated) &&
Objects.equals(admin, that.admin);
}
@Override
public int hashCode() {
return Objects.hash(userId, gender, firstname, lastname, username, email,
password, secQuestion, secAnswer,
salt,
emailValidationCode,
emailEnable,
dateCreated, dateUpdated, admin);
}
@ManyToOne
@JoinColumn(name = "mandant_id", referencedColumnName = "mandant_id")
public MandantEntity getMandant() {
return mandantEntity;
}
public void setMandant(MandantEntity mandant) {
this.mandantEntity = mandant;
}
有什么想法我做错了吗?
答案 0 :(得分:0)
我希望我正确理解了这个问题,希望我的回答对您有所帮助。
query.setParameter([name of parameter], [value])
,它将帮助您进行缩略。该链接的更多详细信息:https://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/ 您的实体现在是users
列表中的对象。您可以将列表定义为所需的对象类型,例如List<UserEntity> users = q.getResultList()
2.a现在,您可以处理列表中的单个元素,例如
if(!users.isEmpty()) {
for(UserEntity uEntity : users){
uEntity.doSomething()
}
return users.get(0)}
我希望这对您有帮助
答案 1 :(得分:-1)
我看了一会儿,我看到这个问题的答案可能与您的问题有关。
最终,这就是您要寻找的答案:
Query.list()返回原始List对象,而不是通用List。
您应该只能将其强制转换为List,并忽略不安全的内容 发出警告。
我认为您混淆了将原始列表与 List元素的实际运行时类型为Object。只是因为 List.get(i)的编译时签名返回一个Object 表示返回的元素的实际类型是Object at 运行时。
贷记给用户“ matt b”,原始答案:https://stackoverflow.com/a/5913021/9891058