0

I'm running a query via Hibernate and it´s returning a list. But the list contains objects instead of my Entities... I'm not a pro in Hibernate, but my other queries work well. I have got no idea what is my mistake right now.

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;

}

My output looks like this:

[int_plan.entity.UserEntity@4b82b237, int_plan.entity.UserEntity@7141a0bb, int_plan.entity.UserEntity@65b0a12c]

My entity looks like that:

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

Any ideas what im doing wrong?

6
  • I don't understand the problem. You are logging your entities, which do not have any toString() method, that's why you see that kind of log. Add a toString() method if you want something more verbose. Commented Oct 22, 2018 at 11:58
  • 1
    I tried to work with toString(), same result.. i've should tell in the Post Commented Oct 22, 2018 at 12:05
  • can you show your UserEntity code? Did you override toString() method in UserEntity class? Commented Oct 22, 2018 at 12:20
  • 1
    no i didn#t overrride to string... Commented Oct 22, 2018 at 12:36
  • 1
    added the Entity code Commented Oct 22, 2018 at 12:38

1 Answer 1

0

I hope I understood the question correctly and that my answer will help you.

  1. Please use query.setParameter([name of parameter], [value]) it will help you with the concationation. more details on that link: https://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/
  2. Your Entitys are now Objects in the users List. you can define your List already to the Objecttype of your desire, for example List<UserEntity> users = q.getResultList()

    2.a now you can adress the single elements in your list like

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

I hope this was helpful for you

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, unfortunately i got the same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.