我无法将List项插入Object

时间:2015-11-15 12:58:25

标签: java spring hibernate list object

我想将List项插入Object,这是代码:

public User login(String username, String password) {
    String stringQuery = "SELECT * FROM User U WHERE U.username = :username AND U.password = :password";
    Query query = HibernateUtil.getCurrentSession().createSQLQuery(stringQuery);
    query.setParameter("username", username);
    query.setParameter("password", password);

    User user;

    List<User> userList = query.list();

    if (userList.size() == 0){
        return null;
    }

    user = (User) userList.get(0);

    return user;
}

这里是User.class

@Entity
public class User {
    private int id;
    private String username;
    private String password;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != user.id) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;
        if (username != null ? !username.equals(user.username) : user.username != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }
}

在这一行我得到错误:

  

java.lang.ClassCastException:[Ljava.lang.Object;无法转换为org.project.server.base.User

user = (User) userList.get(0);

2 个答案:

答案 0 :(得分:1)

根据documentation,使用addEntity

with combos as (
      select id_product_attribute,
             string_agg(id_attribute::text, ',' order by id_attribute) as combo
      from ProductAttributeCombinations pac
      group by id_product_attribute
    )
select *
from combos c
where exists (select 1
              from combos c2
              where c2.id_product_attribute > c.id_product_attribute and
                    c2.combo = c.combo
             );

答案 1 :(得分:0)

如果您确定此查询将返回唯一结果,请使用uniqueResult()

  

返回与查询匹配的单个实例的便捷方法,   如果查询未返回任何结果,则返回null。

public User login(String username, String password) {
    String stringQuery = "FROM User U WHERE U.username = :username AND U.password = :password";
    Query query = HibernateUtil.getCurrentSession().createQuery(stringQuery);
    query.setParameter("username", username);
    query.setParameter("password", password);
    return (User)query.uniqueResult();
}

或者如果你想按自己的方式去做: -

public User login(String username, String password) {
    String stringQuery = "SELECT * FROM User U WHERE U.username = :username AND U.password = :password";
    Query query = HibernateUtil.getCurrentSession().createQuery(stringQuery);
    query.setParameter("username", username);
    query.setParameter("password", password);
    List<User> userList = (ArrayList<User>)query.list();
    if (userList.size() == 0){
        return null;
    }
    return userList.get(0);
}