I have been on this problem for a while, I'm developing a Java Web App on local machine, with MySQL and using EclipseLink (JPA 2.1).
Everything was working just fine and even i was accessing my application, but I realize that the table 'usuario' need other columns on it so in that table 'usuario' I 'Alter' and insert five new columns.
How I insert those new columns on the table I re-generate the Entity Class, but for some reason when I try to login now throws me an error and can't access my application:
Entity Class: (the new columns than I insert are: nombreCompleto, email, direccion, ciudad, estado)
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Ramiro
*/
@Entity
@Table(name = "usuario")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u")
, @NamedQuery(name = "Usuario.findByPkUsuario", query = "SELECT u FROM Usuario u WHERE u.pkUsuario = :pkUsuario")
, @NamedQuery(name = "Usuario.findByUsuario", query = "SELECT u FROM Usuario u WHERE u.usuario = :usuario")
, @NamedQuery(name = "Usuario.findByContrasenia", query = "SELECT u FROM Usuario u WHERE u.contrasenia = :contrasenia")
, @NamedQuery(name = "Usuario.findByFechaCreacion", query = "SELECT u FROM Usuario u WHERE u.fechaCreacion = :fechaCreacion")
, @NamedQuery(name = "Usuario.findByFechaUltimaEdicion", query = "SELECT u FROM Usuario u WHERE u.fechaUltimaEdicion = :fechaUltimaEdicion")
, @NamedQuery(name = "Usuario.findByTipoUsuario", query = "SELECT u FROM Usuario u WHERE u.tipoUsuario = :tipoUsuario")
, @NamedQuery(name = "Usuario.findByEstatus", query = "SELECT u FROM Usuario u WHERE u.estatus = :estatus")
, @NamedQuery(name = "Usuario.findByNombreCompleto", query = "SELECT u FROM Usuario u WHERE u.nombreCompleto = :nombreCompleto")
, @NamedQuery(name = "Usuario.findByEmail", query = "SELECT u FROM Usuario u WHERE u.email = :email")
, @NamedQuery(name = "Usuario.findByDireccion", query = "SELECT u FROM Usuario u WHERE u.direccion = :direccion")
, @NamedQuery(name = "Usuario.findByCiudad", query = "SELECT u FROM Usuario u WHERE u.ciudad = :ciudad")
, @NamedQuery(name = "Usuario.findByEstado", query = "SELECT u FROM Usuario u WHERE u.estado = :estado")})
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "pkUsuario")
private Integer pkUsuario;
@Basic(optional = false)
@Column(name = "usuario")
private String usuario;
@Basic(optional = false)
@Column(name = "contrasenia")
private String contrasenia;
@Basic(optional = false)
@Column(name = "fechaCreacion")
@Temporal(TemporalType.DATE)
private Date fechaCreacion;
@Basic(optional = false)
@Column(name = "fechaUltimaEdicion")
@Temporal(TemporalType.DATE)
private Date fechaUltimaEdicion;
@Basic(optional = false)
@Column(name = "tipoUsuario")
private int tipoUsuario;
@Basic(optional = false)
@Column(name = "estatus")
private int estatus;
@Basic(optional = false)
@Column(name = "nombreCompleto")
private String nombreCompleto;
@Basic(optional = false)
@Column(name = "email")
private String email;
@Basic(optional = false)
@Column(name = "direccion")
private String direccion;
@Basic(optional = false)
@Column(name = "ciudad")
private String ciudad;
@Basic(optional = false)
@Column(name = "estado")
private String estado;
public Usuario() {
}
public Usuario(Integer pkUsuario) {
this.pkUsuario = pkUsuario;
}
public Usuario(Integer pkUsuario, String usuario, String contrasenia, Date fechaCreacion, Date fechaUltimaEdicion, int tipoUsuario, int estatus, String nombreCompleto, String email, String direccion, String ciudad, String estado) {
this.pkUsuario = pkUsuario;
this.usuario = usuario;
this.contrasenia = contrasenia;
this.fechaCreacion = fechaCreacion;
this.fechaUltimaEdicion = fechaUltimaEdicion;
this.tipoUsuario = tipoUsuario;
this.estatus = estatus;
this.nombreCompleto = nombreCompleto;
this.email = email;
this.direccion = direccion;
this.ciudad = ciudad;
this.estado = estado;
}
public Integer getPkUsuario() {
return pkUsuario;
}
public void setPkUsuario(Integer pkUsuario) {
this.pkUsuario = pkUsuario;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getContrasenia() {
return contrasenia;
}
public void setContrasenia(String contrasenia) {
this.contrasenia = contrasenia;
}
public Date getFechaCreacion() {
return fechaCreacion;
}
public void setFechaCreacion(Date fechaCreacion) {
this.fechaCreacion = fechaCreacion;
}
public Date getFechaUltimaEdicion() {
return fechaUltimaEdicion;
}
public void setFechaUltimaEdicion(Date fechaUltimaEdicion) {
this.fechaUltimaEdicion = fechaUltimaEdicion;
}
public int getTipoUsuario() {
return tipoUsuario;
}
public void setTipoUsuario(int tipoUsuario) {
this.tipoUsuario = tipoUsuario;
}
public int getEstatus() {
return estatus;
}
public void setEstatus(int estatus) {
this.estatus = estatus;
}
public String getNombreCompleto() {
return nombreCompleto;
}
public void setNombreCompleto(String nombreCompleto) {
this.nombreCompleto = nombreCompleto;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public String getCiudad() {
return ciudad;
}
public void setCiudad(String ciudad) {
this.ciudad = ciudad;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
@Override
public int hashCode() {
int hash = 0;
hash += (pkUsuario != null ? pkUsuario.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 Usuario)) {
return false;
}
Usuario other = (Usuario) object;
if ((this.pkUsuario == null && other.pkUsuario != null) || (this.pkUsuario != null && !this.pkUsuario.equals(other.pkUsuario))) {
return false;
}
return true;
}
@Override
public String toString() {
return "hg.entity.Usuario[ pkUsuario=" + pkUsuario + " ]";
}
}
Note: If I delete the five new parameters just here on the Entity the error does not appears any more (but I need those parameters).
//Search if the user exist (Here is where the error is thrown)
private List<Usuario> findUsrByUsrPass(String usr, EntityManager em){
List<Usuario> results = em.createNamedQuery("Usuario.findByUser").setParameter("usuario", usr).getResultList();
return results;
}
The error:
[EL Info]: 2017-05-04 18:26:04.041--ServerSession(1801367825)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2017-05-04 18:26:04.598--ServerSession(1801367825)--file:/C:/Program Files (x86)/Apache Software Foundation/Apache Tomcat 8.0.27/temp/15-Hagen/WEB-INF/lib/HagenBack.jar_HagenBackPU login successful
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'ciudad' in 'field list'
Error Code: 1054
Call: SELECT pkUsuario, ciudad, contrasenia, direccion, email, estado, estatus, fechaCreacion, fechaUltimaEdicion, nombreCompleto, tipoUsuario, usuario FROM usuario WHERE (usuario = ?)
bind => [1 parameter bound]
Query: ReadAllQuery(name="Usuario.findByUsuario" referenceClass=Usuario sql="SELECT pkUsuario, ciudad, contrasenia, direccion, email, estado, estatus, fechaCreacion, fechaUltimaEdicion, nombreCompleto, tipoUsuario, usuario FROM usuario WHERE (usuario = ?)")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
I can left those columns on the DB, and if I remove those five new columns of the Entity I can access my application again.
¿What I'm losing? ¿It's there something wrong on my Entity or it is a bug from EclipseLink?
I already try to delete and re-generate the table 'usuario', Cleaning and Building the app, etc; etc; but it looks like MySQL don't refresh the table on the memory or something like that.
I appreciate any help. Regards.
The solution I found it's to delete all tables on the DB and reinserting after that just recreate all the entities (Entity Classes from Database). I know this is a bad solution in future use if the tables has thousands of values, but for the moment, it works. Regards.