矢量无法转换为IUser

时间:2010-10-02 13:26:51

标签: java

===============================

现在我的登录页面有问题;矢量无法转换为IUser

My Stateless Bean

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.DAO;

import com.entity.IUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Kency
 */
@Stateless
@LocalBean
public class UserBean {
    @PersistenceContext(unitName = "mcGrawLibPro-ejbPU")
    private EntityManager em;


    public List<IUser> retrieveAllUser(){
        return em.createNamedQuery("IUser.findAll").getResultList();
    }

     public IUser userLogin(String username, String password){
    Query query = em.createNamedQuery("IUser.findByUsernameAndPassword");
    query.setParameter("username", username);
    query.setParameter("password", password);
    return  (IUser) query.getResultList();
   }

    public void persist(Object object) {
        em.persist(object);
    }

    public IUser findByID(Integer id){
        return em.find(IUser.class, id);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

}

和我的JSF Managed Bean

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mcgraw.controller;

import com.DAO.UserBean;
import com.entity.IUser;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Kency
 */
@ManagedBean
@SessionScoped
public class LoginController implements Serializable{
    @EJB
    private UserBean userBean;
    private IUser user;
    private boolean admin;
    private boolean mod;



    /** Creates a new instance of LoginController */
    public LoginController() {
        user = new IUser();

    }

    //getter / setter
     public boolean isMod() {
        return mod;
    }

    public void setMod(boolean mod) {
        this.mod = mod;
    }


    public IUser getUser() {
        return user;
    }

    public void setUser(IUser user) {
        this.user = user;
    }
    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }




     public String login() {

        user =  userBean.userLogin(user.getUsername(), user.getPassword());
        if (user != null) {
            setAdmin(user.getGroups().getAdmin());
            setMod(user.getGroups().getMod());
            return "home";
        } else {

            return "login";
        }
    }


    public String logout() {
        user = null;
        return "login";
    }

    public boolean isLoggedIn() {
        return user != null;
    }


}

我的实体Bean

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Kency
 */
@Entity
@Table(name = "user")
@NamedQueries({
    @NamedQuery(name = "IUser.findAll", query = "SELECT i FROM IUser i"),
    @NamedQuery(name = "IUser.findByUserid", query = "SELECT i FROM IUser i WHERE i.userid = :userid"),
    @NamedQuery(name = "IUser.findByUsername", query = "SELECT i FROM IUser i WHERE i.username = :username"),
    @NamedQuery(name = "IUser.findByPassword", query = "SELECT i FROM IUser i WHERE i.password = :password"),
    @NamedQuery(name = "IUser.findByEmail", query = "SELECT i FROM IUser i WHERE i.email = :email"),
    @NamedQuery(name = "IUser.findByGender", query = "SELECT i FROM IUser i WHERE i.gender = :gender"),
    @NamedQuery(name = "IUser.findByDob", query = "SELECT i FROM IUser i WHERE i.dob = :dob"),
    @NamedQuery(name = "IUser.findByZipcode", query = "SELECT i FROM IUser i WHERE i.zipcode = :zipcode"),
    @NamedQuery(name = "IUser.findByIstate", query = "SELECT i FROM IUser i WHERE i.istate = :istate"),
    @NamedQuery(name = "IUser.findByAddress", query = "SELECT i FROM IUser i WHERE i.address = :address"),
    @NamedQuery(name = "IUser.findByCity", query = "SELECT i FROM IUser i WHERE i.city = :city"),
    @NamedQuery(name = "IUser.findByUsernameAndPassword", query = "SELECT i FROM IUser i WHERE i.username = :username AND i.password = :password "),
    @NamedQuery(name = "IUser.findByActive", query = "SELECT i FROM IUser i WHERE i.active = :active")})
public class IUser implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "userid")
    private Integer userid;
    @Basic(optional = false)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @Column(name = "gender")
    private String gender;
    @Basic(optional = false)
    @Column(name = "dob")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dob;
    @Basic(optional = false)
    @Column(name = "zipcode")
    private int zipcode;
    @Basic(optional = false)
    @Column(name = "istate")
    private String istate;
    @Basic(optional = false)
    @Column(name = "address")
    private String address;
    @Basic(optional = false)
    @Column(name = "city")
    private String city;
    @Basic(optional = false)
    @Column(name = "active")
    private boolean active;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "iUser")
    private List<Rent> rentList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "iUser")
    private List<Cart> cartList;
    @JoinColumn(name = "igroup", referencedColumnName = "groupsid")
    @ManyToOne(optional = false)
    private Groups groups;

    public IUser() {
    }

    public IUser(Integer userid) {
        this.userid = userid;
    }

     public IUser(String  username, String password) {
       this.username = username;
       this.password = password;
    }

    public IUser(Integer userid, String username, String password, String email, String gender, Date dob, int zipcode, String istate, String address, String city, boolean active) {
        this.userid = userid;
        this.username = username;
        this.password = password;
        this.email = email;
        this.gender = gender;
        this.dob = dob;
        this.zipcode = zipcode;
        this.istate = istate;
        this.address = address;
        this.city = city;
        this.active = active;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public int getZipcode() {
        return zipcode;
    }

    public void setZipcode(int zipcode) {
        this.zipcode = zipcode;
    }

    public String getIstate() {
        return istate;
    }

    public void setIstate(String istate) {
        this.istate = istate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public List<Rent> getRentList() {
        return rentList;
    }

    public void setRentList(List<Rent> rentList) {
        this.rentList = rentList;
    }

    public List<Cart> getCartList() {
        return cartList;
    }

    public void setCartList(List<Cart> cartList) {
        this.cartList = cartList;
    }

    public Groups getGroups() {
        return groups;
    }

    public void setGroups(Groups groups) {
        this.groups = groups;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userid != null ? userid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof IUser)) {
            return false;
        }
        IUser other = (IUser) object;
        if ((this.userid == null && other.userid != null) || (this.userid != null && !this.userid.equals(other.userid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entity.IUser[userid=" + userid + "]";
    }

}

我的转换器类

package com.mcgraw.controller;


import com.entity.IUser;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Kency
 */
@FacesConverter(forClass=IUser.class)
public class UserConverter  implements Converter{
    private IUser user;


    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {


        if(user == null){
        user = new IUser(Integer.valueOf(value));
        return user;
       }else{
        throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to User", value)));

       }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((IUser) value).getUserid());
    }

}

我有Vector的错误无法转换为IUser 我不知道为什么会出错? 我是创建1类来转换IUser,但为什么错误? 但当我将query.getResultlist()更改为==&gt; query.getSingleResult();它运作良好 为什么??? (现在我还没有检查用户登录或者只是尝试创建登录页面)

1 个答案:

答案 0 :(得分:2)

return  (IUser) query.getResultList();

我假设query.getResultList()返回一个Vector。您需要将第一个值拉出并适当地进行投射。您正在投射完整的结果列表。

另请注意,最好检查结果列表是否实际包含1个值(最常见的是仅包含一个值)。