【问题标题】:HibernateQuery returns List of Objects instead of EntitiesHibernateQuery 返回对象列表而不是实体
【发布时间】:2018-10-22 11:53:29
【问题描述】:

我正在通过 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;
}

任何想法我做错了什么?

【问题讨论】:

  • 我不明白这个问题。您正在记录没有任何 toString() 方法的实体,这就是您看到这种日志的原因。如果您想要更详细的内容,请添加 toString() 方法。
  • 我尝试使用 toString(),结果相同.. 我应该在帖子中说明
  • 您能出示您的UserEntity 代码吗?您是否覆盖了 UserEntity 类中的 toString() 方法?
  • 不,我没有#t覆盖到字符串...
  • 添加了实体代码

标签: java orm hibernate-query


【解决方案1】:

我希望我正确理解了这个问题,并且我的回答会对你有所帮助。

  1. 请使用query.setParameter([name of parameter], [value]),它将帮助您进行连接。有关该链接的更多详细信息:https://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/
  2. 您的实体现在是users 列表中的对象。您可以将您的列表定义为您想要的对象类型,例如List<UserEntity> users = q.getResultList()

    2.a 现在您可以像处理列表中的单个元素一样

    if(!users.isEmpty()) {
        for(UserEntity uEntity : users){
            uEntity.doSomething()
        }
        return users.get(0)}
    

希望对你有帮助

【讨论】:

  • 谢谢,不幸的是我得到了同样的结果
猜你喜欢
  • 2014-06-01
  • 2018-12-18
  • 2023-04-04
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2021-07-05
  • 2020-07-17
  • 2012-06-23
相关资源
最近更新 更多