5 Replies Latest reply on Oct 1, 2008 6:15 PM by jschellSomeoneStoleMyAlias

    Query result : Object to DTO : ClassCastException

      hi, my problem is: im using persistence.xml, entity class from db, a controller of persistence class, and my jsp to get the data result in browser, deploy the first time it works fine, but i add something to my code and deploy again and i get ClassCastException when parsing the Object result of the Query to my EmpresasDTO.


      this is my persistence.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="SecureFilePU" transaction-type="JTA">
            <property name="toplink.ddl-generation" value="create-tables"/>
        • 1. Re: Query result : Object to DTO : ClassCastException
          This is my entity class from the db:
          package co.com.itac.securefile.persistence.dto;
          import java.io.Serializable;
          import java.util.Collection;
          import javax.persistence.*;
           * @author marlon
          @Table(name = "empresas")
          @NamedQueries({@NamedQuery(name = "EmpresasDTO.findById", query = "SELECT e FROM EmpresasDTO e WHERE e.id = :id"),
            @NamedQuery(name = "EmpresasDTO.findAll", query = "SELECT e FROM EmpresasDTO e")
          public class EmpresasDTO implements Serializable {
            private static final long serialVersionUID = 2L;
            @Column(name = "id", nullable = false)
            private Integer id;
            @Column(name = "nombre", nullable = false)
            private String nombre;
            @Column(name = "campo1")
            private String campo1;
            @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEmpresa")
            private Collection<EstadoCargaDTO> estadoCargaDTOCollection;
            @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEmpresa")
            private Collection<EstadoDescargaDTO> estadoDescargaDTOCollection;
            @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEmpresa")
            private Collection<UsuariosDTO> usuariosDTOCollection;
            public EmpresasDTO() {
            public EmpresasDTO(Integer id) {
              this.id = id;
            public EmpresasDTO(Integer id, String nombre) {
              this.id = id;
              this.nombre = nombre;
            public Integer getId() {
              return id;
            public void setId(Integer id) {
              this.id = id;
            public String getNombre() {
              return nombre;
            public void setNombre(String nombre) {
              this.nombre = nombre;
            public String getCampo1() {
              return campo1;
            public Collection<EstadoCargaDTO> getEstadoCargaDTOCollection() {
              return estadoCargaDTOCollection;
            public void setEstadoCargaDTOCollection(Collection<EstadoCargaDTO> estadoCargaDTOCollection) {
              this.estadoCargaDTOCollection = estadoCargaDTOCollection;
            public Collection<EstadoDescargaDTO> getEstadoDescargaDTOCollection() {
              return estadoDescargaDTOCollection;
            public void setEstadoDescargaDTOCollection(Collection<EstadoDescargaDTO> estadoDescargaDTOCollection) {
              this.estadoDescargaDTOCollection = estadoDescargaDTOCollection;
            public Collection<UsuariosDTO> getUsuariosDTOCollection() {
              return usuariosDTOCollection;
            public void setUsuariosDTOCollection(Collection<UsuariosDTO> usuariosDTOCollection) {
              this.usuariosDTOCollection = usuariosDTOCollection;
            public int hashCode() {
              int hash = 0;
              hash += (id != null ? id.hashCode() : 0);
              return hash;
            public boolean equals(Object object) {
              // TODO: Warning - this method won't work in the case the id fields are not set
              if (!(object instanceof EmpresasDTO)) {
                return false;
              EmpresasDTO other = (EmpresasDTO) object;
              if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
              return true;
            public String toString() {
              return "co.com.itac.securefile.persistence.dto.EmpresasDTO[id=" + id + "]";
          in my PersistenceController:
            public EmpresasDTO consultarEmpresa(Integer id) {
              try {
                Query consulta = em.createNamedQuery("EmpresasDTO.findById");
                consulta.setParameter("id", id);
                EmpresasDTO respuesta = (EmpresasDTO) consulta.getSingleResult();
                return respuesta;
              } finally {
          ERROR Result:
          Caused by: java.lang.ClassCastException
               at java.lang.Class.cast(Class.java:2951)
               at co.com.itac.securefile.persistence.controller.ControladoraPersistencia.consultarEmpresas(ControladoraPersistencia.java:204)
               at co.com.itac.securefileserveradmin.AdminSessionBean.inicializaListaEmpresas(AdminSessionBean.java:142)
               at co.com.itac.securefileserveradmin.portal.empresas.ConsultaEmpresas.prerender(ConsultaEmpresas.java:143)
               at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.prerender(ViewHandlerImpl.java:806)
               at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:303)
               ... 39 more
          thnaks x the help.
          • 2. Re: Query result : Object to DTO : ClassCastException
            when i restart glassfish, works fine the first time run application, the second deploy crash.
            i HAVE to restart glassfish everytime i do changes :(
            • 3. Re: Query result : Object to DTO : ClassCastException
              I suggest that you print out the type of the class before the cast.
              • 4. Re: Query result : Object to DTO : ClassCastException
                jschell, that was the first i did, when the Object Query results i ask if(Object instanceof EmpresaDTO) it returns false, else{ print out Object.getClass and EmpresaDTO.class } and i get: co.com.itac.securefile.persistence.EmpresaDTO in both cases. what is wrong?.
                • 5. Re: Query result : Object to DTO : ClassCastException
                  i get: co.com.itac.securefile.persistence.EmpresaDTO in both cases. what is wrong?.
                  Simple. The explanation for that sort of result is always the same.

                  There are two different classloaders involved. You can figure out which ones via the 'class'
                  of each although that probably won't help you much. Your solution will probably revolve
                  around configuration and or packaging (how you put the classes together.)