0

I'm trying to receive a list of objects of type Tempfiles and I'm not sure what has gone wrong in the way of fetching it. and when I receive the list, the size of the list is not zero but when I try to use it, I'm getting a ClassCastException : Here's the pojo of the class Tempfiles :

@Entity
@Table(name="tempfiles")
@NamedQuery(name="Tempfile.findAll", query="SELECT t FROM Tempfile t")
public class Tempfile implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer tempfilesid;

    private byte[] filestream;

    //bi-directional many-to-one association to Filemetadata
    @ManyToOne
    @JoinColumn(name="documentid")
    private Filemetadata filemetadata;

    public Tempfile() {
    }

    public Integer getTempfilesid() {
        return this.tempfilesid;
    }

    public void setTempfilesid(Integer tempfilesid) {
        this.tempfilesid = tempfilesid;
    }

    public byte[] getFilestream() {
        return this.filestream;
    }

    public void setFilestream(byte[] filestream) {
        this.filestream = filestream;
    }

    public Filemetadata getFilemetadata() {
        return this.filemetadata;
    }

    public void setFilemetadata(Filemetadata filemetadata) {
        this.filemetadata = filemetadata;
    }
}

Here's the method that I use to :

public List getTempFileData()
    {
        String METHOD_NAME = "getTempFileData";
        logger.logEntering(METHOD_NAME);
        String strQueryToGetStream = "select t.tempfilesid, t.filemetadata.documentid, t.filestream from Tempfile t";
        logger.logInfo(strQueryToGetStream);
        Query query = entityManager.createQuery(strQueryToGetStream);
        List <Tempfile>tempFileList = query.getResultList();
        logger.logExiting(METHOD_NAME);
        return tempFileList;
    }

Here's how I receive it :

List <Tempfile>tempFileList = fileDao.getTempFileData();

And I get the exception when I use the list in a loop :

for(Tempfile tempFile : tempFileList)
{
    decryptAndEncryptStream(tempFile, job, publicKey);
}

And this is the exception I'm getting :

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.global.empris.domain.Tempfile

Would greatly appreciate your help and thanks in advance.

1
  • Reading of any JPA docs would reveal that when you specify a SELECT clause selecting fields of the candidate then your return type is List<Object[]>. Commented Apr 9, 2018 at 7:11

3 Answers 3

1

Your query is returning a list of Object[] instead of TempFile. You are only choosing specific columns, so it won't be mapped to the full object.

Process each Object[] like you would a resultset, or change your query to select a full object (i.e. SELECT t FROM Tempfile t).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response and ya initially I casted the list of Object[] to type TempFile. That wouldn't work too is it?
Of course it wouldn't work. Casting isn't magic transformation. Either query the full object and handle that, or query specific columns and handle those. Depending on your needs.
0

The statement List <Tempfile>tempFileList = query.getResultList(); returns a List<Object>. You should us Transformers.aliasToBean(TempFile.class)

Comments

0

IMO, you're getting an array of objects from JPA, because you only selected a portion of your entity. If you want your entity, select it and fetch its children collections like so

select t from Tempfile t left join fetch t.filestream

2 Comments

As I remember (might be a little rusty), you don't need to add the select t when only 1 entire entity is selected... Might be wrong
Well MB then, it is true that I'm more used to Hibernate than full JPA... correcting the JPQL right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.