我在让这个程序工作时遇到了问题。我收到错误,我无法弄清楚原因。
这是我的主要代码
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByIdUser", query = "SELECT u FROM Users u WHERE u.idUser = :idUser"),
@NamedQuery(name = "Users.findByNome", query = "SELECT u FROM Users u WHERE u.nome = :nome"),
@NamedQuery(name = "Users.findByDataNasc", query = "SELECT u FROM Users u WHERE u.dataNasc = :dataNasc"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_user")
private Integer idUser;
@Basic(optional = false)
@Column(name = "nome")
private String nome;
@Basic(optional = false)
@Column(name = "data_nasc")
private String dataNasc;
@Basic(optional = false)
@Column(name = "email")
private String email;
public Users() {
}
public Users(Integer idUser) {
this.idUser = idUser;
}
public Users(Integer idUser, String nome, String dataNasc, String email) {
this.idUser = idUser;
this.nome = nome;
this.dataNasc = dataNasc;
this.email = email;
}
public Integer getIdUser() {
return idUser;
}
public void setIdUser(Integer idUser) {
this.idUser = idUser;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getDataNasc() {
return dataNasc;
}
public void setDataNasc(String dataNasc) {
this.dataNasc = dataNasc;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idUser != null ? idUser.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 Users)) {
return false;
}
Users other = (Users) object;
if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser))) {
return false;
}
return true;
}
@Override
public String toString() {
return "database.Users[ idUser=" + idUser + " ]";
}
}
这是发生错误的Java文件
public class TabelaUsers extends AbstractTableModel {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EsmaiorPU");
private final List listagem;
private final ArrayList<Users>lista = new ArrayList();
public TabelaUsers() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
listagem = em.createQuery("Select t from Users t").getResultList();
for (Iterator<String> it = listagem.iterator(); it.hasNext();) {
String elem = it.next();
lista.add((Users) em.createNamedQuery("Users.findByDataNasc").setParameter("dataNasc", elem).getSingleResult());
}
em.close();
}
@Override
public int getRowCount() {
return lista.size();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex==0){
return lista.get(rowIndex).getNome();
}else if(columnIndex==1){
return lista.get(rowIndex).getDataNasc();
}else if(columnIndex==2){
return lista.get(rowIndex).getEmail();
}
return null;
}
}
这一行发生了错误
String elem = it.next();
抛出以下异常
线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.ClassCastException:model.Users无法强制转换为java.lang.String 我失去了,有什么帮助吗?
谢谢
答案 0 :(得分:0)
我最好的猜测是:em.createQuery(...)。getResultList()in:
listagem = em.createQuery("Select t from Users t").getResultList();
正在返回List<Users>
。因此,当您从此List
获取迭代器时,迭代器包含Users
。由于Users
不是String
的子类(因为没有任何内容可以是String
的子类),因此您获得了ClassCastException
。
答案 1 :(得分:0)
您需要更改查询以获取dataNasc而不是完整对象。
listagem = em.createQuery("Select t.dataNasc from Users t").getResultList()
或者更好地使用TypedQuery来避免类型转换。
List<String> listagem = em.createQuery("Select t.dataNasc from Users t", String.class).getResultList()